{"id":1679,"date":"2016-11-08T17:33:28","date_gmt":"2016-11-08T17:33:28","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/?p=845"},"modified":"2019-02-20T10:45:59","modified_gmt":"2019-02-20T17:45:59","slug":"typescript-2-1-rc-better-inference-async-functions-and-more-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/typescript-2-1-rc-better-inference-async-functions-and-more-2\/","title":{"rendered":"TypeScript 2.1 RC: Better Inference, Async Functions, and More"},"content":{"rendered":"<p>Today we&#8217;re happy to announce our release candidate for TypeScript 2.1! If you aren&#8217;t familiar with it, <a href=\"https:\/\/typescriptlang.org\/\">TypeScript<\/a> is a language that adds optional static types to JavaScript, and brings new features from ES6 and later to whatever JavaScript runtime you&#8217;re using.<\/p>\n<p>As usual you can get the RC through NuGet, or just by running<\/p>\n<div class=\"highlight highlight-source-shell\">\n<pre>npm install -g typescript@rc<\/pre>\n<\/div>\n<p>You can then easily use the RC release with <a href=\"https:\/\/code.visualstudio.com\/docs\/languages\/typescript#_using-newer-typescript-versions\">Visual Studio Code<\/a> or <a href=\"https:\/\/github.com\/Microsoft\/TypeScript-Sublime-Plugin\/#note-using-different-versions-of-typescript\">our Sublime Text Plugin<\/a>.<\/p>\n<p>You can also grab <a href=\"http:\/\/download.microsoft.com\/download\/6\/D\/8\/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA\/TS2.1-dev14update3-20161206.2\/TypeScript_Dev14Full.exe\">the TypeScript 2.1 installer for Visual Studio 2015<\/a> after getting <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt752379.aspx\">Update 3<\/a>.<\/p>\n<p>While TypeScript 2.1 has a lot of great features coming up, we&#8217;d like to highlight how much more powerful TypeScript 2.1&#8217;s inference will be, as well as how much easier it will be to write asynchronous code in all runtimes.<\/p>\n<h2><a id=\"user-content-smarter-inference\" class=\"anchor\"><\/a>Smarter Inference<\/h2>\n<p>TypeScript 2.1 now makes it easier to model scenarios where you might <em>incrementally<\/em> initialize variables. Since a lot of code is written like this in JavaScript, this makes it even easier to migrate existing codebases to TypeScript.<\/p>\n<p>To understand better, let&#8217;s start off by talking about the <code>any<\/code> type.<\/p>\n<p>Most of the time, if TypeScript can&#8217;t figure out the type of a variable, it will choose the <code>any<\/code> type to be as flexible as possible without troubling you. We often call this an <em>implicit<\/em> <code>any<\/code> (as opposed to an <em>explicit<\/em> one, where you would have written out the type).<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #00f\">let<\/span> x;      <span style=\"color: #148A14\">\/\/ implicitly 'any'<\/span>\n<span style=\"color: #00f\">let<\/span> y <span>=<\/span> []; <span style=\"color: #148A14\">\/\/ implicitly 'any[]'<\/span>\n\n<span style=\"color: #00f\">let<\/span> z<span style=\"color: #00f\">:<\/span> <span style=\"color: #267F99\">any<\/span>; <span style=\"color: #148A14\">\/\/ explicitly 'any'.<\/span><\/pre>\n<\/div>\n<p>From that point on, you can do anything you want with those values. For many people, that behavior was too loose, which is why the <code>--noImplicitAny<\/code> option will warn you whenever a type couldn&#8217;t be inferred.<\/p>\n<p>With TypeScript 2.0 we built out a foundation of using control flow analysis to track the flow of types throughout your program. Because that analysis examines the assignments of every variable, we&#8217;ve leveraged that same foundation in TypeScript 2.1 to more deeply examine the type of any variable that seems like it&#8217;s destined for a better type. Instead of just choosing <code>any<\/code>, TypeScript will infer types based on what you end up assigning later on.<\/p>\n<p>Let&#8217;s take the following example.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #00f\">let<\/span> x;\n\n<span style=\"color: #148A14\">\/\/ We can still assign anything we want to 'x'.<\/span>\n<span class=\"pl-en\">x<\/span> <span>=<\/span> () <span>=&gt;<\/span> <span style=\"color: #09885A\">42<\/span>;\n\n<span style=\"color: #148A14\">\/\/ After that last assignment, TypeScript 2.1 knows that 'x'<\/span>\n<span style=\"color: #148A14\">\/\/ has the type '() =&gt; number', so it can be called.<\/span>\n<span class=\"pl-en\">x<\/span>();\n\n<span style=\"color: #148A14\">\/\/ But now we'll get an error that we can't add a number to a function!<\/span>\n<span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span class=\"pl-smi\">x<\/span> <span>+<\/span> <span style=\"color: #09885A\">42<\/span>);\n<span style=\"color: #148A14\">\/\/          ~~~~~~<\/span>\n<span style=\"color: #148A14\">\/\/ error: Operator '+' cannot be applied to types '() =&gt; number' and 'number'.<\/span>\n\n<span style=\"color: #148A14\">\/\/ TypeScript still allows us to assign anything we want to 'x'.<\/span>\n<span class=\"pl-smi\">x<\/span> <span>=<\/span> <span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>Hello world!<span class=\"pl-pds\">\"<\/span><\/span>;\n\n<span style=\"color: #148A14\">\/\/ But now it also knows that now 'x' is a 'string'!<\/span>\n<span class=\"pl-smi\">x<\/span>.<span class=\"pl-c1\">toLowerCase<\/span>();<\/pre>\n<\/div>\n<p>When it comes to assignments, TypeScript will still trust you and allow you to assign anything you want to <code>x<\/code>. However, for any other uses, the type checker will know better by climbing up and looking at whatever you&#8217;ve actually done with <code>x<\/code>.<\/p>\n<p>The same sort of tracking is now also done for empty arrays! This means better completions:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #00f\">let<\/span> puppies <span>=<\/span> [];\n\n<span class=\"pl-smi\">puppies<\/span>.<span class=\"pl-c1\">push<\/span>(<span style=\"color: #00f\">new<\/span> <span class=\"pl-en\">Puppy<\/span>());\n\n<span style=\"color: #00f\">for<\/span> (<span style=\"color: #00f\">let<\/span> pup <span style=\"color: #00f\">of<\/span> <span class=\"pl-smi\">puppies<\/span>) {\n    <span class=\"pl-smi\">pup<\/span>.<span class=\"pl-en\">bark<\/span>();\n    <span style=\"color: #148A14\">\/\/  ^^^^ Get completion on 'bark'<\/span>\n}<\/pre>\n<\/div>\n<p>And it also means that TypeScript can catch more obvious errors:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span class=\"pl-smi\">puppies<\/span>[<span style=\"color: #09885A\">1<\/span>] <span>=<\/span> <span style=\"color: #00f\">new<\/span> <span class=\"pl-en\">Kitty<\/span>();\n\n<span style=\"color: #00f\">for<\/span> (<span style=\"color: #00f\">let<\/span> pup <span style=\"color: #00f\">of<\/span> <span class=\"pl-smi\">puppies<\/span>) {\n    <span class=\"pl-smi\">pup<\/span>.<span class=\"pl-en\">bark<\/span>();\n    <span style=\"color: #148A14\">\/\/  ~~~~ error: 'bark' does not exist on type 'Kitty'<\/span>\n}<\/pre>\n<\/div>\n<p><video style=\"width: 75%\" autoplay=\"autoplay\" loop=\"loop\" src=\"http:\/\/devblogs.microsoft.com\/typescript\/wp-content\/uploads\/sites\/11\/2016\/11\/cfa-any-array-puppy-kitty-dark22.mp4\">\n<\/video><\/p>\n<p>The end result of all this is that you&#8217;ll see <em>way fewer<\/em> implicit <code>any<\/code> errors in the future, and get much better tooling support.<\/p>\n<h2><a id=\"user-content-downlevel-async-functions\" class=\"anchor\"><\/a>Downlevel Async Functions<\/h2>\n<p>Support for down-level asynchronous functions (or async\/await) is coming in 2.1, and you can use it in today&#8217;s release candidate! <code>async<\/code>\/<code>await<\/code> is a new feature in ECMAScript 2017 that allows users to write code around promises without needing to use callbacks. <code>async<\/code> functions can be written in a style that looks synchronous, but acts asynchronously, using the <code>await<\/code> keyword.<\/p>\n<p>This feature was supported before TypeScript 2.1, but only when targeting ES6\/ES2015. TypeScript 2.1 brings the capability to ES3 and ES5 runtimes, meaning you&#8217;ll be free to take advantage of it no matter what environment you&#8217;re using.<\/p>\n<p>For example, let&#8217;s take the following function named <code>delay<\/code>, which returns a promise and waits for a certain amount of time before resolving:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #00f\">function<\/span> delay(<span style=\"color: #000000\">milliseconds<\/span><span style=\"color: #00f\">:<\/span> <span style=\"color: #267F99\">number<\/span>) {\n    <span style=\"color: #00f\">return<\/span> <span style=\"color: #00f\">new<\/span> <span class=\"pl-en\">Promise<\/span>&lt;<span style=\"color: #267F99\">void<\/span>&gt;(<span style=\"color: #000000\">resolve<\/span> <span>=&gt;<\/span> {\n      <span class=\"pl-c1\">setTimeout<\/span>(<span class=\"pl-smi\">resolve<\/span>, <span class=\"pl-smi\">milliseconds<\/span>);\n    });\n}<\/pre>\n<\/div>\n<p>Let&#8217;s try to work on a simple-sounding task. We want to write a program that prints <code>\"Hello\"<\/code>, three dots, and then <code>\"World!\"<\/code>.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #00f\">function<\/span> welcome() {\n    <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>Hello<span class=\"pl-pds\">\"<\/span><\/span>);\n\n    <span style=\"color: #00f\">for<\/span> (<span style=\"color: #00f\">let<\/span> i <span>=<\/span> <span style=\"color: #09885A\">0<\/span>; <span class=\"pl-smi\">i<\/span> <span style=\"color: #00f\">&lt;<\/span> <span style=\"color: #09885A\">3<\/span>; <span class=\"pl-smi\">i<\/span><span>++<\/span>) {\n        <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>.<span class=\"pl-pds\">\"<\/span><\/span>);\n    }\n\n    <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>World!<span class=\"pl-pds\">\"<\/span><\/span>);\n}<\/pre>\n<\/div>\n<p>This turned out to be about as simple as it sounded.<\/p>\n<p>Now let&#8217;s say we want to use our <code>delay<\/code> function to pause before each dot.<\/p>\n<p>Without <code>async<\/code>\/<code>await<\/code>, we&#8217;d have to write something like the following:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #00f\">function<\/span> dramaticWelcome() {\n    <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>Hello<span class=\"pl-pds\">\"<\/span><\/span>);\n\n    (<span style=\"color: #00f\">function<\/span> loop(<span style=\"color: #000000\">i<\/span>){\n        <span style=\"color: #00f\">if<\/span> (<span class=\"pl-smi\">i<\/span> <span style=\"color: #00f\">&lt;<\/span> <span style=\"color: #09885A\">3<\/span>) {\n            <span class=\"pl-en\">delay<\/span>(<span style=\"color: #09885A\">500<\/span>).<span class=\"pl-en\">then<\/span>(() <span>=&gt;<\/span> {\n                <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>.<span class=\"pl-pds\">\"<\/span><\/span>);\n                <span class=\"pl-en\">loop<\/span>(<span class=\"pl-smi\">i<\/span> <span>+<\/span> <span style=\"color: #09885A\">1<\/span>);\n            });\n        }\n        <span style=\"color: #00f\">else<\/span> {\n            <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>World!<span class=\"pl-pds\">\"<\/span><\/span>);\n        }\n    })(<span style=\"color: #09885A\">0<\/span>);\n}<\/pre>\n<\/div>\n<p>This doesn&#8217;t look quite as simple any more! What about if we tried using <code>async<\/code> functions to make this code more readable?<\/p>\n<p>First, we need to make sure our runtime has an ECMAScript-compliant <code>Promise<\/code> available globally. That might involve grabbing <a href=\"https:\/\/github.com\/stefanpenner\/es6-promise\">a polyfill<\/a> for <code>Promise<\/code>, or relying on one that you might have in the runtime that you&#8217;re targeting. We also need to make sure that TypeScript knows <code>Promise<\/code> exists by setting our <code>lib<\/code> flag to something like <code>\"dom\", \"es2015\"<\/code> or <code>\"dom\", \"es2015.promise\", \"es5\"<\/code>:<\/p>\n<div class=\"highlight highlight-source-json\">\n<pre>{\n    <span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>compilerOptions<span class=\"pl-pds\">\"<\/span><\/span>: {\n        <span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>lib<span class=\"pl-pds\">\"<\/span><\/span>: [<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>dom<span class=\"pl-pds\">\"<\/span><\/span>, <span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>es2015.promise<span class=\"pl-pds\">\"<\/span><\/span>, <span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>es5<span class=\"pl-pds\">\"<\/span><\/span>]\n    }\n}<\/pre>\n<\/div>\n<p>Now we can rewrite our code to use <code>async<\/code> and <code>await<\/code>:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #00f\">async<\/span> <span style=\"color: #00f\">function<\/span> dramaticWelcome() {\n    <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>Hello<span class=\"pl-pds\">\"<\/span><\/span>);\n\n    <span style=\"color: #00f\">for<\/span> (<span style=\"color: #00f\">let<\/span> i <span>=<\/span> <span style=\"color: #09885A\">0<\/span>; <span class=\"pl-smi\">i<\/span> <span style=\"color: #00f\">&lt;<\/span> <span style=\"color: #09885A\">3<\/span>; <span class=\"pl-smi\">i<\/span><span>++<\/span>) {\n        <span style=\"color: #00f\">await<\/span> <span class=\"pl-en\">delay<\/span>(<span style=\"color: #09885A\">500<\/span>);\n        <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>.<span class=\"pl-pds\">\"<\/span><\/span>);\n    }\n\n    <span class=\"pl-c1\">console<\/span>.<span class=\"pl-c1\">log<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>World!<span class=\"pl-pds\">\"<\/span><\/span>);\n}<\/pre>\n<\/div>\n<p>Notice how similar this is compared to our synchronous version! Despite its looks, this function is actually <em>asynchronous<\/em>, and won&#8217;t block other code from running in between each pause. In fact, the two versions of <code>dramaticWelcome<\/code> basically boil down to the same code, but with <code>async<\/code> &amp; <code>await<\/code>, TypeScript does the heavy lifting for us.<\/p>\n<h2><a id=\"user-content-next-steps\" class=\"anchor\"><\/a>Next Steps<\/h2>\n<p>TypeScript 2.1 RC has plenty of other features, and we&#8217;ll have even more coming for 2.1 proper. You can take a look at <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/Roadmap\">our roadmap<\/a> to see what else is in store. We hope you give it a try and enjoy it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re happy to announce our release candidate for TypeScript 2.1! If you aren&#8217;t familiar with it, TypeScript is a language that adds optional static types to JavaScript, and brings new features from ES6 and later to whatever JavaScript runtime you&#8217;re using. As usual you can get the RC through NuGet, or just by running [&hellip;]<\/p>\n","protected":false},"author":381,"featured_media":1797,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>Today we&#8217;re happy to announce our release candidate for TypeScript 2.1! If you aren&#8217;t familiar with it, TypeScript is a language that adds optional static types to JavaScript, and brings new features from ES6 and later to whatever JavaScript runtime you&#8217;re using. As usual you can get the RC through NuGet, or just by running [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/users\/381"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/comments?post=1679"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1679\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/media\/1797"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/media?parent=1679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=1679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=1679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}