{"id":925,"date":"2017-02-02T18:59:11","date_gmt":"2017-02-02T18:59:11","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/?p=925"},"modified":"2019-02-20T10:45:57","modified_gmt":"2019-02-20T17:45:57","slug":"announcing-typescript-2-2-rc","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-2-2-rc\/","title":{"rendered":"Announcing TypeScript 2.2 RC"},"content":{"rendered":"<p>TypeScript 2.2 is just around the corner, and today we&#8217;re announcing its release candidate!<\/p>\n<p>If you&#8217;re first hearing about it, TypeScript is a language that just takes JavaScript and adds optional static types. Being built on JavaScript means that you don&#8217;t have to learn much more beyond what you know from JavaScript, and all your existing code continues to work with TypeScript. Meanwhile, the optional types that TypeScript adds can help you catch pesky bugs and work more efficiently by enabling great tooling support.<\/p>\n<p>To try out the RC today, you can use <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.TypeScript.MSBuild\/2.2.0\">NuGet<\/a>, or using npm just run<\/p>\n<div class=\"highlight highlight-source-shell\">\n<pre>npm install -g typescript@rc<\/pre>\n<\/div>\n<p>You can also get the TypeScript release candidate <a href=\"http:\/\/download.microsoft.com\/download\/6\/D\/8\/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA\/TS-2.2-dev14update3-20170201.4\/TypeScript_Dev14Full.exe\">for Visual Studio 2015<\/a> (if you have <a href=\"https:\/\/www.visualstudio.com\/en-us\/news\/releasenotes\/vs2015-update3-vs\">Update 3<\/a>). Other built-in editor support will be coming with our proper 2.2 release, but you can look at guides on how to enable newer versions of TypeScript in <a href=\"https:\/\/code.visualstudio.com\/Docs\/languages\/typescript#_using-newer-typescript-versions\">Visual Studio Code<\/a> and <a href=\"https:\/\/github.com\/Microsoft\/TypeScript-Sublime-Plugin\/#note-using-different-versions-of-typescript\">Sublime Text 3<\/a>.<\/p>\n<p>To clue you in on what&#8217;s new, here&#8217;s a few noteworthy features to get an idea about what to keep an eye out for in this release candidate.<\/p>\n<h2>The <code style=\"font-size: 29px\">object<\/code> type<\/h2>\n<p>There are times where an API allows you to pass in any sort of value <em>except<\/em> for a primitive. For example, consider <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/create\"><code>Object.create<\/code><\/a>, which throws an exception unless you pass in an object or <code>null<\/code> for its first argument.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #148A14\">\/\/ All of these throw errors at runtime!<\/span>\n<span style=\"color: #267F99\">Object<\/span>.<span class=\"pl-en\">create<\/span>(<span class=\"pl-c1\">undefined<\/span>);\n<span style=\"color: #267F99\">Object<\/span>.<span class=\"pl-en\">create<\/span>(<span style=\"color: #09885A\">1000<\/span>);\n<span style=\"color: #267F99\">Object<\/span>.<span class=\"pl-en\">create<\/span>(<span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>hello world<span class=\"pl-pds\">\"<\/span><\/span>);<\/pre>\n<\/div>\n<p>If we try to come up with the type of the first parameter, a naive approach might be <code>Object | null<\/code>. Unfortunately, this doesn&#8217;t quite work. Because of the way that <a href=\"https:\/\/en.wikipedia.org\/wiki\/Structural_type_system\">structural types<\/a> work in TypeScript, <code>number<\/code>, <code>string<\/code>, and <code>boolean<\/code> are all assignable to <code>Object<\/code>.<\/p>\n<p>To address this, we&#8217;ve created the new <code>object<\/code> type (and notice all of those lowercase letters!).\nA more obvious name might be the &#8220;non-primitive&#8221; type.<\/p>\n<p>The <code>object<\/code> type is &#8220;empty&#8221; &#8211; it has no properties just like the <code>{}<\/code> type.\nThat means that almost everything is assignable to <code>object<\/code> <em>except<\/em> for primitives.\nIn other words, <code>number<\/code>, <code>boolean<\/code>, <code>string<\/code>, <code>symbol<\/code>, <code>null<\/code>, and <code>undefined<\/code> won&#8217;t be compatible.<\/p>\n<p>But that means that we can now correctly type <code>Object.create<\/code>&#8216;s first parameter as <code>object | null<\/code>!<\/p>\n<p>We anticipate that the <code>object<\/code> primitive type will help catch a large class of bugs, and more accurately model real world code.<\/p>\n<p>We owe a big thanks to <a href=\"https:\/\/github.com\/HerringtonDarkholme\/\">Herrington Darkholme<\/a> for lending a hand and implementing this feature!<\/p>\n<h2>Improved support for mixins and composable classes<\/h2>\n<p>The mixin pattern is fairly common in the JavaScript ecosystem, and in TypeScript 2.2 we&#8217;ve made some adjustments in the language to supporting it even better.<\/p>\n<p>To get this done, we removed some restrictions on classes in TypeScript 2.2, like being able to extend from a value that constructs an intersection type. It also adjusts some functionality in the way that signatures on intersection types get combined. The result is that you can write a function that<\/p>\n<ol>\n<li>takes a constructor<\/li>\n<li>declares a class that extends that constructor<\/li>\n<li>adds members to that new class<\/li>\n<li>and returns the class itself.<\/li>\n<\/ol>\n<p>For example, we can write the <code>Timestamped<\/code> function which takes a class, and extends it by adding a <code>timestamp<\/code> member.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #148A14\">\/** Any type that can construct *something*. *\/<\/span>\n<span style=\"color: #0000ff\">export<\/span> <span style=\"color: #0000ff\">type<\/span> <span style=\"color: #267F99\">Constructable<\/span> = <span style=\"color: #0000ff\">new<\/span> (...<span class=\"pl-v\">args<\/span>: <span style=\"color: #0000ff\">any<\/span>[]) =&gt; <span style=\"color: #0000ff\">{}<\/span>;\n\n<span style=\"color: #0000ff\">export<\/span> <span style=\"color: #0000ff\">function<\/span> <span style=\"color: #267F99\">Timestamped<\/span>&lt;<span style=\"color: #267F99\">BC<\/span> <span style=\"color: #0000ff\">extends<\/span> <span style=\"color: #267F99\">Constructable<\/span>&gt;(<span class=\"pl-v\">Base<\/span>: <span style=\"color: #267F99\">BC<\/span>) {\n    <span style=\"color: #0000ff\">return<\/span> <span style=\"color: #0000ff\">class<\/span> <span style=\"color: #0000ff\">extends<\/span> <span class=\"pl-e\">Base<\/span> {\n        timestamp = <span style=\"color: #0000ff\">new<\/span> <span style=\"color: #267F99\">Date<\/span>();\n    };\n}<\/pre>\n<\/div>\n<p>Now we can take any class and pass it through <code>Timestamped<\/code> to quickly compose a new type.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">class<\/span> <span style=\"color: #267F99\">Point<\/span> {\n    x: <span style=\"color: #0000ff\">number<\/span>;\n    y: <span style=\"color: #0000ff\">number<\/span>;\n    <span style=\"color: #0000ff\">constructor<\/span>(<span class=\"pl-v\">x<\/span>: <span style=\"color: #0000ff\">number<\/span>, <span class=\"pl-v\">y<\/span>: <span style=\"color: #0000ff\">number<\/span>) {\n        <span style=\"color: #0000ff\">this<\/span>.<span class=\"pl-c1\">x<\/span> = <span class=\"pl-smi\">x<\/span>;\n        <span style=\"color: #0000ff\">this<\/span>.<span class=\"pl-c1\">y<\/span> = <span class=\"pl-smi\">y<\/span>;\n    }\n}\n\n<span style=\"color: #0000ff\">const<\/span> <span style=\"color: #267F99\">TimestampedPoint<\/span> = <span style=\"color: #267F99\">Timestamped<\/span>(<span style=\"color: #267F99\">Point<\/span>);\n\n<span style=\"color: #0000ff\">const<\/span> p = <span style=\"color: #0000ff\">new<\/span> <span style=\"color: #267F99\">TimestampedPoint<\/span>(<span style=\"color: #09885A\">10<\/span>, <span style=\"color: #09885A\">10<\/span>);\n<span class=\"pl-smi\">p<\/span>.<span class=\"pl-c1\">x<\/span> + <span class=\"pl-smi\">p<\/span>.<span class=\"pl-c1\">y<\/span>;\n<span class=\"pl-smi\">p<\/span>.<span class=\"pl-smi\">timestamp<\/span>.<span class=\"pl-c1\">getMilliseconds<\/span>();<\/pre>\n<\/div>\n<p>Similarly, we could write a <code>Tagged<\/code> function which adds a <code>tag<\/code> member. These functions actually make it very easy to compose extensions. Making a special 3D point that&#8217;s tagged and timestamped is pretty clean.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">class<\/span> <span style=\"color: #267F99\">SpecialPoint<\/span> <span style=\"color: #0000ff\">extends<\/span> <span style=\"color: #267F99\">Tagged<\/span>(<span style=\"color: #267F99\">Timestamped<\/span>(<span class=\"pl-c1\">Point<\/span>)) {\n    z: <span class=\"pl-c1\">number<\/span>;\n    <span style=\"color: #0000ff\">constructor<\/span>(<span class=\"pl-v\">x<\/span>: <span class=\"pl-c1\">number<\/span>, <span class=\"pl-v\">y<\/span>: <span class=\"pl-c1\">number<\/span>, <span class=\"pl-v\">z<\/span>: <span class=\"pl-c1\">number<\/span>) {\n        <span style=\"color: #0000ff\">super<\/span>(<span class=\"pl-smi\">x<\/span>, <span class=\"pl-smi\">y<\/span>);\n        <span style=\"color: #0000ff\">this<\/span>.<span class=\"pl-c1\">z<\/span> = <span class=\"pl-smi\">z<\/span>;\n    }\n}<\/pre>\n<\/div>\n<h2><\/a>A new JSX emit mode: <code style=\"font-size: 29px\">react-native<\/code><\/h2>\n<p>In TypeScript 2.1 and earlier, the <code>--jsx<\/code> flag could take on two values:<\/p>\n<ul>\n<li><code>preserve<\/code> which leaves JSX syntax alone and generates <code>.jsx<\/code> files.<\/li>\n<li><code>react<\/code> which transforms JSX into calls to <code>React.createElement<\/code> and generates <code>.js<\/code> files.<\/li>\n<\/ul>\n<p>TypeScript 2.2 has a new JSX emit mode called <code>react-native<\/code> which sits somewhere in the middle. Under this scheme, JSX syntax is left alone, but generates <code>.js<\/code> files.<\/p>\n<p>This new mode accomodates React Native&#8217;s loader which expects all input files to be <code>.js<\/code> files. It also satisfies cases where you want to just leave your JSX syntax alone but get <code>.js<\/code> files out from TypeScript.<\/p>\n<h2>What&#8217;s next for 2.2?<\/h2>\n<p>While we can&#8217;t list everything that we&#8217;ve worked on here on this post, you can see what else we have in store <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/Roadmap\">here on the TypeScript Roadmap<\/a>.<\/p>\n<p>We&#8217;re counting on your feedback to make TypeScript 2.2 a solid release!\nPlease feel free to leave us feedback here, or <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/issues\/new\">file an issue on GitHub<\/a> if you run into anything you may think is a bug.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript 2.2 is just around the corner, and today we&#8217;re announcing its release candidate! If you&#8217;re first hearing about it, TypeScript is a language that just takes JavaScript and adds optional static types. Being built on JavaScript means that you don&#8217;t have to learn much more beyond what you know from JavaScript, and all your [&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-925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>TypeScript 2.2 is just around the corner, and today we&#8217;re announcing its release candidate! If you&#8217;re first hearing about it, TypeScript is a language that just takes JavaScript and adds optional static types. Being built on JavaScript means that you don&#8217;t have to learn much more beyond what you know from JavaScript, and all your [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/925","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=925"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/925\/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=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}