{"id":1677,"date":"2016-07-11T16:28:51","date_gmt":"2016-07-11T16:28:51","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/?p=705"},"modified":"2019-02-20T10:46:02","modified_gmt":"2019-02-20T17:46:02","slug":"announcing-typescript-2-0-beta-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-2-0-beta-2\/","title":{"rendered":"Announcing TypeScript 2.0 Beta"},"content":{"rendered":"<p>Today we&#8217;re excited to roll out our beta release of TypeScript 2.0. If you&#8217;re not familiar with TypeScript yet, you can start learning it today on <a href=\"https:\/\/www.typescriptlang.org\/\">our website<\/a>.<\/p>\n<p>To get your hands on the beta, you can download <a href=\"http:\/\/download.microsoft.com\/download\/6\/D\/8\/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA\/TS2.0.0-TS-release20-nightly-20160708.1\/TypeScript_Dev14Full.exe\">TypeScript 2.0 Beta for Visual Studio 2015<\/a> (which will require <a href=\"https:\/\/www.visualstudio.com\/en-us\/news\/releasenotes\/vs2015-update3-vs\">VS 2015 Update 3<\/a>), or just run<\/p>\n<div class=\"highlight highlight-source-shell\">\n<pre>npm install -g typescript@beta<\/pre>\n<\/div>\n<p>This release includes plenty of new features, such as <a href=\"https:\/\/blogs.msdn.microsoft.com\/typescript\/2016\/06\/15\/the-future-of-declaration-files\/\">our new workflow for getting <code>.d.ts<\/code> files<\/a>, but here&#8217;s a couple more features just to get an idea of what else is in store.<\/p>\n<h2 id=\"non-nullable-types\">Non-nullable Types<\/h2>\n<p><code>null<\/code> and <code>undefined<\/code> are two of the most common sources of bugs in JavaScript. Before TypeScript 2.0, <code>null<\/code> and <code>undefined<\/code> were in the domain of <em>every<\/em> type. That meant that if you had a function that took a <code>string<\/code>, you couldn&#8217;t be sure from the type alone of whether you actually had a <code>string<\/code> &#8211; you might actually have <code>null<\/code>.<\/p>\n<p>In TypeScript 2.0, the new <code>--strictNullChecks<\/code> flag changes that. <code>string<\/code> just means <code>string<\/code> and <code>number<\/code> means <code>number<\/code>.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">let<\/span> <span class=\"pl-v\">foo<\/span>: <span style=\"color: #0000ff\">string<\/span> = <span style=\"color: #0000ff\">null<\/span>; <span style=\"color: #148A14\">\/\/ Error!<\/span><\/pre>\n<\/div>\n<p>What if you <em>wanted<\/em> to make something nullable? Well we&#8217;ve brought two new types to the scene: <code>null<\/code> and <code>undefined<\/code>. As you might expect, <code>null<\/code> can only contain <code>null<\/code>, and <code>undefined<\/code> only contains <code>undefined<\/code>. They&#8217;re not totally useful on their own, but you can use them in a union type to describe whether something <em>could<\/em> be <code>null<\/code>\/<code>undefined<\/code>.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">let<\/span> <span class=\"pl-v\">foo<\/span>: <span style=\"color: #0000ff\">string<\/span> | <\/span><span style=\"color: #0000ff\">null<\/span> = <span style=\"color: #0000ff\">null<\/span>; <span style=\"color: #148A14\">\/\/ Okay!<\/span><\/pre>\n<\/div>\n<p>Because you might often know better than the type system, we&#8217;ve also introduced a postfix <code>!<\/code> operator that takes <code>null<\/code> and <code>undefined<\/code> out of the type of any expression.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">let<\/span> <span class=\"pl-v\">strs<\/span>: <span style=\"color: #0000ff\">string<\/span>[] | <span style=\"color: #0000ff\">undefined<\/span>;\n\n<span style=\"color: #148A14\">\/\/ Error! 'strs' is possibly undefined.<\/span>\n<span style=\"color: #0000ff\">let<\/span> <span class=\"pl-v\">upperCased<\/span> = strs.map(s =&gt; s.toUpperCase());\n\n<span style=\"color: #148A14\">\/\/ 'strs!' means we're sure it can't be 'undefined', so we can call 'map' on it.<\/span>\n<span style=\"color: #0000ff\">let<\/span> <span class=\"pl-v\">lowerCased<\/span> = strs!.map(s =&gt; s.toLowerCase());<\/pre>\n<\/div>\n<h2><a id=\"user-content-control-flow-analysis-for-types\" class=\"anchor\" href=\"https:\/\/gist.github.com\/DanielRosenwasser\/dfe91fb1d64f1205c1886407010756fa#control-flow-analysis-for-types\"><\/a>Control Flow Analysis for Types<\/h2>\n<p>TypeScript&#8217;s support for handling nullable types is possible thanks to changes in how types are tracked throughout the program. In 2.0, we&#8217;ve started using control flow analysis to better understand what a type has to be at a given location. For instance, consider this function.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #148A14\">\/**<\/span>\n<span style=\"color: #148A14\"> * @param recipients  An array of recipients, or a comma-separated list of recipients.<\/span>\n<span style=\"color: #148A14\"> * @param body        Primary content of the message.<\/span>\n<span style=\"color: #148A14\"> *\/<\/span>\n<span style=\"color: #0000ff\">function<\/span> <span class=\"pl-en\">sendMessage<\/span>(<span class=\"pl-v\">recipients<\/span>: <span style=\"color: #0000ff\">string<\/span> | <span style=\"color: #0000ff\">string<\/span>[], <span class=\"pl-v\">body<\/span>: <span style=\"color: #0000ff\">string<\/span>) {\n    <span style=\"color: #0000ff\">if<\/span> (<span style=\"color: #0000ff\">typeof<\/span> recipients === <span style=\"color: #a31515\">\"string\"<\/span>) {\n        recipients = recipients.split(<span style=\"color: #a31515\">\",\"<\/span>);\n    }\n\n    <span style=\"color: #148A14\">\/\/ TypeScript knows that 'recipients' is a 'string[]' here.<\/span>\n    recipients = recipients.filter(isValidAddress);\n    <span style=\"color: #0000ff\">for<\/span> (<span style=\"color: #0000ff\">let<\/span> r <span style=\"color: #0000ff\">of<\/span> recipients) {\n        <span style=\"color: #148A14\">\/\/ ...<\/span>\n    }\n}<\/pre>\n<\/div>\n<p>Notice that after the assignment within the <code>if<\/code> block, TypeScript understood that it had to be dealing with an array of <code>string<\/code>s.\nThis sort of thing can catch issues early on and save you from spending time on debugging.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">let<\/span> <span class=\"pl-v\">bestItem<\/span>: Item;\n<span style=\"color: #0000ff\">for<\/span> (<span style=\"color: #0000ff\">let<\/span> item <span style=\"color: #0000ff\">of<\/span> items) {\n    <span style=\"color: #0000ff\">if<\/span> (item.id === <span style=\"color: #09885A\">42<\/span>) bestItem = item;\n}\n\n<span style=\"color: #148A14\">\/\/ Error! 'bestItem' might not have been initialized if 'items' was empty.<\/span>\n<span style=\"color: #0000ff\">let<\/span> <span class=\"pl-v\">itemName<\/span> = bestItem.name;<\/pre>\n<\/div>\n<p>We owe a major thanks to <a href=\"https:\/\/github.com\/ivogabe\">Ivo Gabe de Wolff<\/a> for his work and involvement in implementing this feature, which started out with his thesis project and grew into part of TypeScript itself.<\/p>\n<h2><a id=\"user-content-easier-module-declarations\" class=\"anchor\" href=\"https:\/\/gist.github.com\/DanielRosenwasser\/dfe91fb1d64f1205c1886407010756fa#easier-module-declarations\"><\/a>Easier Module Declarations<\/h2>\n<p>Sometimes you want to just tell TypeScript that a module exists, and you might not care what its shape is. It used to be that you&#8217;d have to write something like this:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">module<\/span> <span style=\"color: #a31515\">\"foo\"<\/span> {\n    <span style=\"color: #0000ff\">var<\/span> <span class=\"pl-v\">x<\/span>: <span style=\"color: #0000ff\">any<\/span>;\n    <span style=\"color: #0000ff\">export<\/span> = x;\n}<\/pre>\n<\/div>\n<p>But that&#8217;s a hassle, so we made it easier and got rid of the boilerplate. In TypeScript 2.0 you can just write<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">module<\/span> <span style=\"color: #a31515\">\"foo\"<\/span>;\n<span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">module<\/span> <span style=\"color: #a31515\">\"bar\"<\/span>;<\/pre>\n<\/div>\n<p>When you&#8217;re ready to finally outline the shape of a module, you can come back to these declarations and define the structure you need.<\/p>\n<p>What if you you depend on a package with lots of modules? Writing those out for each module might be a pain, but TypeScript 2.0 makes that easy too by allowing wildcards in these declarations!<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">module<\/span> <span style=\"color: #a31515\">\"foo\/*\"<\/span>;<\/pre>\n<\/div>\n<p>Now you can import any path that starts with <code>foo\/<\/code> and TypeScript will assume it exists. You can take advantage of this if your module loader understands how to import based on a certain patterns too. For example:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">module<\/span> <span style=\"color: #a31515\">\"*!text\"<\/span> {\n    <span style=\"color: #0000ff\">const<\/span> <span class=\"pl-v\">content<\/span>: <span style=\"color: #0000ff\">string<\/span>;\n    <span style=\"color: #0000ff\">export<\/span> = content;\n}<\/pre>\n<\/div>\n<p>Now whenever you import a path ending with <code>!text<\/code>, TypeScript will understand that the import should be typed as a <code>string<\/code>.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">import<\/span> text = require(<span style=\"color: #a31515\">\".\/hello.txt!text\"<\/span>);\ntext.toLowerCase();<\/pre>\n<\/div>\n<h2><a id=\"user-content-next-steps\" class=\"anchor\" href=\"https:\/\/gist.github.com\/DanielRosenwasser\/dfe91fb1d64f1205c1886407010756fa#next-steps\"><\/a>Next Steps<\/h2>\n<p>One feature you might be wondering about is support for <code>async<\/code> functions in ES3 &amp; ES5. Originally, this was slated for the 2.0 release; however, to reasonably implement <code>async<\/code>\/<code>await<\/code>, we needed to rewrite TypeScript&#8217;s emitter as a series of tree transformations. Doing so, while keeping TypeScript fast, has required a lot of work and attention to detail. While we feel confident in today&#8217;s implementation, confidence is no match for thorough testing, and more time is needed for <code>async<\/code>\/<code>await<\/code>to stabilize. You can expect it in TypeScript 2.1, and if you&#8217;d like to track the progress, <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/pull\/9175\">the pull request is currently open on GitHub<\/a>.<\/p>\n<p>TypeScript 2.0 is still packed with many useful new features, and we&#8217;ll be coming out with more details as time goes on. If you&#8217;re curious to hear more about what&#8217;s new, you can <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What%27s-new-in-TypeScript\">take a look at our wiki<\/a>. In the coming weeks, a more stable release candidate will be coming out, with the final product landing not too far after.<\/p>\n<p>We&#8217;d love to hear any feedback you have, either in the comments below or on GitHub. Happy hacking!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re excited to roll out our beta release of TypeScript 2.0. If you&#8217;re not familiar with TypeScript yet, you can start learning it today on our website. To get your hands on the beta, you can download TypeScript 2.0 Beta for Visual Studio 2015 (which will require VS 2015 Update 3), or just run [&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-1677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>Today we&#8217;re excited to roll out our beta release of TypeScript 2.0. If you&#8217;re not familiar with TypeScript yet, you can start learning it today on our website. To get your hands on the beta, you can download TypeScript 2.0 Beta for Visual Studio 2015 (which will require VS 2015 Update 3), or just run [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1677","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=1677"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1677\/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=1677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=1677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=1677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}