{"id":1678,"date":"2016-08-30T16:12:07","date_gmt":"2016-08-30T16:12:07","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/?p=725"},"modified":"2019-02-20T10:46:01","modified_gmt":"2019-02-20T17:46:01","slug":"announcing-typescript-2-0-rc-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-2-0-rc-2\/","title":{"rendered":"Announcing TypeScript 2.0 RC"},"content":{"rendered":"<p>TypeScript 2.0 is <em>almost<\/em> out, and today we&#8217;re happy to show just how close we are with our release candidate! If you haven&#8217;t used TypeScript yet, check out <a href=\"https:\/\/www.typescriptlang.org\/docs\/tutorial.html\">the intro tutorial on our website<\/a> to get started.<\/p>\n<p>To start using the RC now, you can download <a href=\"http:\/\/download.microsoft.com\/download\/6\/D\/8\/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA\/TS2.0.2-TS-release20-nightly-20160828.1\/TypeScript_Dev14Full.exe\">TypeScript 2.0 RC for Visual Studio 2015<\/a> (which requires <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt752379.aspx\">VS Update 3<\/a>), grab it through NuGet, or use npm:<\/p>\n<pre><code>npm install -g typescript@rc\n<\/code><\/pre>\n<p>Visual Studio Code users can <a href=\"https:\/\/code.visualstudio.com\/docs\/languages\/typescript#_using-newer-typescript-versions\">follow the steps here<\/a> to use the RC.<\/p>\n<p>This RC gives an idea of what the full version of 2.0 will look like, and we&#8217;re looking for broader feedback to stabilize and make 2.0 a solid release. Overall, the RC should be stable enough for general use, and we don&#8217;t expect any major new features to be added past this point.<\/p>\n<p>On the other hand, lots of stuff has been added since 2.0 beta was released, so here&#8217;s a few features that you might not have heard about since then.<\/p>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/1d8230ed6376de515942e82a45888a47#tagged-unions\" id=\"user-content-tagged-unions\" class=\"anchor\"><\/a>Tagged Unions<\/h2>\n<p>Tagged unions are an exciting new feature that brings functionality from languages like F#, Swift, Rust, and others to JavaScript, while embracing the way that people write JavaScript today. This feature is also called <em>discriminated unions<\/em>, <em>disjoint unions<\/em>, or <em>algebraic data types<\/em>. But what&#8217;s in the feature is much more interesting than what&#8217;s in the name.<\/p>\n<p>Let&#8217;s say you have two types: <code>Circle<\/code> and <code>Square<\/code>. You then have a union type of the two named <code>Shape<\/code>.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">interface<\/span> <span style=\"color: #267F99\">Circle<\/span> {\n    <span style=\"color: #000000\">kind<\/span>: <span style=\"color: #a31515\">\"circle\"<\/span>;\n    <span style=\"color: #000000\">radius<\/span>: <span style=\"color: #0000ff\">number<\/span>;\n}\n\n<span style=\"color: #0000ff\">interface<\/span> <span style=\"color: #267F99\">Square<\/span> {\n    <span style=\"color: #000000\">kind<\/span>: <span style=\"color: #a31515\">\"square\"<\/span>;\n    <span style=\"color: #000000\">sideLength<\/span>: <span style=\"color: #0000ff\">number<\/span>;\n}\n\n<span style=\"color: #0000ff\">type<\/span> <span style=\"color: #267F99\">Shape<\/span> <span style=\"color: #0000ff\">= <\/span><span style=\"color: #267F99\">Circle<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #267F99\">Square<\/span>;<\/pre>\n<\/div>\n<p>Notice that both <code>Circle<\/code> and <code>Square<\/code> have a field named <code>kind<\/code> which has a string literal type. That means the <code>kind<\/code> field on a <code>Circle<\/code> will always contain the string <code>\"circle\"<\/code>. Each type has a common field, but has been <em>tagged<\/em> with a unique value.<\/p>\n<p>In TypeScript 1.8, writing a function to get the area of a <code>Shape<\/code> required a type assertions for each type in <code>Shape<\/code>.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">function<\/span> <span class=\"pl-en\">getArea<\/span>(<span style=\"color: #000000\">shape<\/span>: <span style=\"color: #267F99\">Shape<\/span>) {\n    <span style=\"color: #0000ff\">switch<\/span> (shape.kind) {\n        <span style=\"color: #0000ff\">case<\/span> <span style=\"color: #a31515\">\"circle\"<\/span>:\n            <span style=\"color: #148A14\">\/\/ Convert from 'Shape' to 'Circle'<\/span>\n            <span style=\"color: #0000ff\">let<\/span> <span style=\"color: #000000\">c<\/span> = shape <span style=\"color: #0000ff\">as<\/span> <span style=\"color: #267F99\">Circle<\/span>;\n            <span style=\"color: #0000ff\">return<\/span> Math.PI <span style=\"color: #0000ff\">*<\/span> c.radius <span style=\"color: #0000ff\">**<\/span> <span style=\"color: #09885A\">2<\/span>;\n\n        <span style=\"color: #0000ff\">case<\/span> <span style=\"color: #a31515\">\"square\"<\/span>:\n            <span style=\"color: #148A14\">\/\/ Convert from 'Shape' to 'Square'<\/span>\n            <span style=\"color: #0000ff\">let<\/span> <span style=\"color: #000000\">sq<\/span> = shape <span style=\"color: #0000ff\">as<\/span> <span style=\"color: #267F99\">Square<\/span>;\n            <span style=\"color: #0000ff\">return<\/span> sq.sideLength <span style=\"color: #0000ff\">**<\/span> <span style=\"color: #09885A\">2<\/span>;\n    }\n}<\/pre>\n<\/div>\n<p>Notice we made up intermediate variables for <code>shape<\/code> just to keep this a little cleaner.<\/p>\n<p>In 2.0, that isn&#8217;t necessary. The language <em>understands<\/em> how to discriminate based on the <code>kind<\/code> field, so you can cut down on the boilerplate.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">function<\/span> <span class=\"pl-en\">getArea<\/span>(<span style=\"color: #000000\">shape<\/span>: <span style=\"color: #267F99\">Shape<\/span>) {\n    <span style=\"color: #0000ff\">switch<\/span> (shape.kind) {\n        <span style=\"color: #0000ff\">case<\/span> <span style=\"color: #a31515\">\"circle\"<\/span>:\n            <span style=\"color: #148A14\">\/\/ 'shape' is a 'Circle' here.<\/span>\n            <span style=\"color: #0000ff\">return<\/span> Math.PI <span style=\"color: #0000ff\">*<\/span> shape.radius <span style=\"color: #0000ff\">**<\/span> <span style=\"color: #09885A\">2<\/span>;\n\n        <span style=\"color: #0000ff\">case<\/span> <span style=\"color: #a31515\">\"square\"<\/span>:\n            <span style=\"color: #148A14\">\/\/ 'shape' is a 'Square' here.<\/span>\n            <span style=\"color: #0000ff\">return<\/span> shape.sideLength <span style=\"color: #0000ff\">**<\/span> <span style=\"color: #09885A\">2<\/span>;\n    }\n}<\/pre>\n<\/div>\n<p>This is totally valid, and TypeScript can use control flow analysis to figure out the type at each branch. In fact, you can use <code>--noImplicitReturns<\/code> and the upcoming <code>--strictNullChecks<\/code> feature to make sure these checks are exhaustive.<\/p>\n<p>Tagged unions make it way easier to get type safety using JavaScript patterns you&#8217;d write today. For example, libraries like Redux will often use this pattern when processing actions.<\/p>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/1d8230ed6376de515942e82a45888a47#more-literal-types\" id=\"user-content-more-literal-types\" class=\"anchor\"><\/a>More Literal Types<\/h2>\n<p>String literal types are a feature we showed off back in 1.8, and were tremendously useful. Like you saw above, we were able to leverage them to bring you tagged unions.<\/p>\n<p>We wanted to give some more love to types other than just <code>string<\/code>. In 2.0, each unique <code>boolean<\/code>, <code>number<\/code>, and enum member will have its own type!<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">type<\/span> <span style=\"color: #267F99\">Digit<\/span> <span style=\"color: #0000ff\">= <\/span><span style=\"color: #09885A\">0<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">1<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">2<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">3<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">4<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">5<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">6<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">7<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">8<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #09885A\">9<\/span>;\n<span style=\"color: #0000ff\">let<\/span> <span style=\"color: #000000\">nums<\/span>: <span style=\"color: #267F99\">Digit<\/span>[] <span style=\"color: #0000ff\">=<\/span> [<span style=\"color: #09885A\">1<\/span>, <span style=\"color: #09885A\">2<\/span>, <span style=\"color: #09885A\">4<\/span>, <span style=\"color: #09885A\">8<\/span>];\n\n<span style=\"color: #148A14\">\/\/ Error! '16' isn't a 'Digit'!<\/span>\nnums.push(<span style=\"color: #09885A\">16<\/span>);<\/pre>\n<\/div>\n<p>Using tagged unions, we can express some things a little more naturally.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">interface<\/span> <span style=\"color: #267F99\">Success<\/span>&lt;<span style=\"color: #267F99\">T<\/span>&gt; {\n    <span style=\"color: #000000\">success<\/span>: <span style=\"color: #0000ff\">true<\/span>;\n    <span style=\"color: #000000\">value<\/span>: <span style=\"color: #267F99\">T<\/span>;\n}\n\n<span style=\"color: #0000ff\">interface<\/span> <span style=\"color: #267F99\">Failure<\/span> {\n    <span style=\"color: #000000\">success<\/span>: <span style=\"color: #0000ff\">false<\/span>;\n    <span style=\"color: #000000\">reason<\/span>: <span style=\"color: #0000ff\">string<\/span>;\n}\n\n<span style=\"color: #0000ff\">type<\/span> <span style=\"color: #267F99\">Result<\/span>&lt;<span style=\"color: #267F99\">T<\/span>&gt; <span style=\"color: #0000ff\">= <\/span><span style=\"color: #267F99\">Success<\/span>&lt;<span style=\"color: #267F99\">T<\/span>&gt; <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #267F99\">Failure<\/span>;<\/pre>\n<\/div>\n<p>The <code>Result&lt;T&gt;<\/code> type here represents something that can potentially fail. If it succeeds, it has a <code>value<\/code>, and if it fails, it contains a reason for failure. The <code>value<\/code> field can only be used when <code>success<\/code> is <code>true<\/code>.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">function<\/span> <span class=\"pl-en\">tryGetNumUsers<\/span>(): <span style=\"color: #267F99\">Result<\/span>&lt;<span style=\"color: #0000ff\">number<\/span>&gt;;\n\n<span style=\"color: #0000ff\">let<\/span> <span style=\"color: #000000\">result<\/span> = tryGetNumUsers();\n<span style=\"color: #0000ff\">if<\/span> (result.success <span style=\"color: #0000ff\">===<\/span> <span style=\"color: #0000ff\">true<\/span>) {\n    <span style=\"color: #148A14\">\/\/ 'result' has type 'Success&lt;number&gt;'<\/span>\n    console.log(<span style=\"color: #a31515\">`Server reported <\/span><span style=\"color: #0000ff\">${<\/span>result.value<span style=\"color: #0000ff\">}<\/span><span style=\"color: #a31515\"> users`<\/span>);\n}\n<span style=\"color: #0000ff\">else<\/span> {\n    <span style=\"color: #148A14\">\/\/ 'result' has type 'Failure'<\/span>\n    console.error(<span style=\"color: #a31515\">\"Error fetching number of users!\"<\/span>, result.reason);\n}<\/pre>\n<\/div>\n<p>You may&#8217;ve noticed that enum members get their own type too!<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">enum<\/span> <span style=\"color: #267F99\">ActionType<\/span> { Append, Erase }\n\n<span style=\"color: #0000ff\">interface<\/span> <span style=\"color: #267F99\">AppendAction<\/span> { \n    <span style=\"color: #000000\">type<\/span>: ActionType.Append;\n    <span style=\"color: #000000\">text<\/span>: <span style=\"color: #267F99\">string<\/span>;\n}\n\n<span style=\"color: #0000ff\">interface<\/span> <span style=\"color: #267F99\">EraseAction<\/span> {\n    <span style=\"color: #000000\">type<\/span>: ActionType.Erase;\n    <span style=\"color: #000000\">numChars<\/span>: <span style=\"color: #0000ff\">number<\/span>;\n}\n\n<span style=\"color: #0000ff\">function<\/span> <span class=\"pl-en\">updateText<\/span>(<span style=\"color: #000000\">currentText<\/span>: <span style=\"color: #267F99\">string<\/span>, <span style=\"color: #000000\">action<\/span>: <span style=\"color: #267F99\">AppendAction<\/span> <span style=\"color: #0000ff\">|<\/span> <span style=\"color: #267F99\">EraseAction<\/span>) {\n    <span style=\"color: #0000ff\">if<\/span> (action.type <span style=\"color: #0000ff\">===<\/span> ActionType.Append) {\n        <span style=\"color: #148A14\">\/\/ 'action' has type 'AppendAction'<\/span>\n        <span style=\"color: #0000ff\">return<\/span> currentText <span style=\"color: #0000ff\">+<\/span> action.text;\n    }\n    <span style=\"color: #0000ff\">else<\/span> {\n        <span style=\"color: #148A14\">\/\/ 'action' has type 'EraseAction'<\/span>\n        <span style=\"color: #0000ff\">return<\/span> currentText.slice(<span style=\"color: #09885A\">0<\/span>, <span style=\"color: #0000ff\">-<\/span>action.numChars);\n    }\n}<\/pre>\n<\/div>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/1d8230ed6376de515942e82a45888a47#globs-includes-and-excludes\" id=\"user-content-globs-includes-and-excludes\" class=\"anchor\"><\/a>Globs, Includes, and Excludes<\/h2>\n<p>When we first introduced the <code>tsconfig.json<\/code> file, you told us that manually listing files was a pain. TypeScript 1.6 introduced the <code>exclude<\/code> field to alleviate this; however, the consensus has been that this was just not enough. It&#8217;s a pain to write out every single file path, and you can run into issues when you forget to exclude new files.<\/p>\n<p>TypeScript 2.0 finally adds support for globs. Globs allow us to write out wildcards for paths, making them as granular as you need without being tedious to write.<\/p>\n<p>You can use them in the new <code>include<\/code> field as well as the existing <code>exclude<\/code> field. As an example, let&#8217;s take a look at this <code>tsconfig.json<\/code> that compiles all our code except for our tests:<\/p>\n<div class=\"highlight highlight-source-json\">\n<pre>{\n    <span style=\"color: #a31515\"><span style=\"color: #a31515\">\"<\/span>include<span style=\"color: #a31515\">\"<\/span><\/span>: [\n        <span style=\"color: #a31515\"><span style=\"color: #a31515\">\"<\/span>.\/src\/**\/*.ts<span style=\"color: #a31515\">\"<\/span><\/span>\n    ],\n    <span style=\"color: #a31515\"><span style=\"color: #a31515\">\"<\/span>exclude<span style=\"color: #a31515\">\"<\/span><\/span>: [\n        <span style=\"color: #a31515\"><span style=\"color: #a31515\">\"<\/span>.\/src\/tests\/**<span style=\"color: #a31515\">\"<\/span><\/span>\n    ]\n}<\/pre>\n<\/div>\n<p>TypeScript&#8217;s globs support the following wildcards:<\/p>\n<ul>\n<li><code>*<\/code> for 0 or more non-separator characters (such as <code>\/<\/code> or <code>\\<\/code>).<\/li>\n<li><code>?<\/code> to match exactly one non-separator character.<\/li>\n<li><code>**\/<\/code> for any number of subdirectories<\/li>\n<\/ul>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/1d8230ed6376de515942e82a45888a47#next-steps\" id=\"user-content-next-steps\" class=\"anchor\"><\/a>Next Steps<\/h2>\n<p>Like we mentioned, TypeScript 2.0 is not far off, but using the RC along with 2.0&#8217;s new features will play a huge part in that release for the broader community.<\/p>\n<p>Feel free to reach out to us about any issues <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\">through GitHub<\/a>. We would love to hear any and all feedback as you try things out. Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript 2.0 is almost out, and today we&#8217;re happy to show just how close we are with our release candidate! If you haven&#8217;t used TypeScript yet, check out the intro tutorial on our website to get started. To start using the RC now, you can download TypeScript 2.0 RC for Visual Studio 2015 (which requires [&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-1678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>TypeScript 2.0 is almost out, and today we&#8217;re happy to show just how close we are with our release candidate! If you haven&#8217;t used TypeScript yet, check out the intro tutorial on our website to get started. To start using the RC now, you can download TypeScript 2.0 RC for Visual Studio 2015 (which requires [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1678","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=1678"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1678\/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=1678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=1678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=1678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}