{"id":1197,"date":"2017-10-12T19:01:18","date_gmt":"2017-10-12T19:01:18","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/?p=1197"},"modified":"2019-02-20T10:45:45","modified_gmt":"2019-02-20T17:45:45","slug":"announcing-typescript-2-6-rc","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-2-6-rc\/","title":{"rendered":"Announcing TypeScript 2.6 RC"},"content":{"rendered":"<div style=\"font-size: 16px\">\n<\/div>\n<p>TypeScript 2.6 RC is now available! To get started with the latest stable version of TypeScript, you can grab it <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.TypeScript.MSBuild\">through NuGet<\/a>, or use the following command with npm:<\/p>\n<div class=\"highlight highlight-source-shell\">\n<pre>npm install -g typescript@rc<\/pre>\n<\/div>\n<p>Visual Studio 2015 users (who have <a href=\"https:\/\/www.visualstudio.com\/en-us\/news\/releasenotes\/vs2015-update3-vs\">Update 3<\/a>)  can <a href=\"http:\/\/download.microsoft.com\/download\/6\/D\/8\/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA\/2.6.0-TS-release-dev14update3-20171007.2\/TypeScript_Dev14Full.exe\">install TypeScript 2.6 RC from here<\/a>, and Visual Studio 2017 users using version 15.2 or later will be able to get TypeScript <a href=\"http:\/\/download.microsoft.com\/download\/7\/0\/A\/70A6AC0E-8934-4396-A43E-445059F430EA\/2.6.0-TS-release-dev14update3-20171007.2\/TypeScript_SDK.exe\">by simply installing it from here<\/a>.<\/p>\n<p>You can get TypeScript 2.6 RC working with <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<\/a>, though the full release of TypeScript 2.6 will be available out of the box.<\/p>\n<p>Let&#8217;s take a look at what TypeScript 2.6 will bring!<\/p>\n<h2> Contravariant function parameters with <code style=\"color: #a31515;font-size: 29px\">strictFunctionTypes<\/code><\/h2>\n<p>With TypeScript 2.6, we&#8217;re introducing a new <code style=\"color: #a31515\">--strict<\/code> mode flag: <code style=\"color: #a31515\">--strictFunctionTypes<\/code>. TypeScript has traditionally compared parameters in a bivariant manner. There are a number of reasons for this &#8211; modeling event handlers in the DOM, allowing a simpler model for working with arrays, and more.<\/p>\n<p><a href=\"http:\/\/www.typescriptlang.org\/docs\/handbook\/release-notes\/typescript-2-4.html#strict-contravariance-for-callback-parameters\">In TypeScript 2.4<\/a>, we tightened these checks a bit, but a few problems still remained. For example, the following would compile with no errors:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">function<\/span> makeLowerCase(<span class=\"pl-v\">s<\/span><span class=\"pl-k\">:<\/span> <span style=\"color: #0000ff\">string<\/span>) {\n    <span style=\"color: #0000ff\">return<\/span> <span class=\"pl-smi\">s<\/span>.<span class=\"pl-c1\">toLowerCase<\/span>();\n}\n\n<span style=\"color: #0000ff\">declare<\/span> <span style=\"color: #0000ff\">let<\/span> foo<span class=\"pl-k\">:<\/span> <span style=\"color: #267F99\">Promise<\/span>&lt;<span style=\"color: #0000ff\">string<\/span> <span class=\"pl-k\">|<\/span> <span style=\"color: #0000ff\">number<\/span>&gt;;\n<span class=\"pl-smi\">foo<\/span>.<span class=\"pl-en\">then<\/span>(<span class=\"pl-smi\">makeLowerCase<\/span>); <span style=\"color: #148A14\">\/\/ Whoops!<\/span><\/pre>\n<\/div>\n<p>While this is a very basic example, we re-evaluated our design decisions based on how JavaScript is written today and will be in the coming years, and felt that there were improvements we could make while accomodating the patterns described above.<\/p>\n<p>Under <code style=\"color: #a31515\">--strictFunctionTypes<\/code>, any function type that doesn&#8217;t originate from a method has its parameters compared <em>contravariantly<\/em>. That means that the above code will fail, because when we try to pass <code style=\"color: #a31515\">makeLowerCase<\/code> with the type <code style=\"color: #a31515\">(s: string) =&gt; string<\/code> to <code style=\"color: #a31515\">then<\/code> which expects a function of type <code style=\"color: #a31515\">(onfulfilled: string | number) =&gt; ...<\/code>, we&#8217;ll flip directions and try to assess whether <code style=\"color: #a31515\">string | number<\/code> is assignable to <code style=\"color: #a31515\">string<\/code> which it isn&#8217;t.<\/p>\n<p>The compromise of increasing strictness on all function types but methods also allows TypeScript to continue modeling the above use-cases (e.g. event handlers and simpler array handling).<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Covariance_and_contravariance_(computer_science)\">Covariance and contravariance<\/a> probably deserve a a more thorough explanation. If you&#8217;d like to read a bit more, some of us on the team felt that <a href=\"https:\/\/www.stephanboyer.com\/post\/132\/what-are-covariance-and-contravariance\">this article<\/a> gives a reasonable high-level explanation. You can also read up more on <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/pull\/18654\">the original pull request<\/a>. But the short version is that with <code style=\"color: #a31515\">--strictFunctionTypes<\/code>, you&#8217;ll be able to catch many common mistakes.<\/p>\n<p>Keep in mind that while <code style=\"color: #a31515\">--strictFunctionTypes<\/code> is opt-in, it will automatically be turned on if you&#8217;re operating with the <code style=\"color: #a31515\">--strict<\/code> flag on. This may introduce some breaks, so to disable the check with <code style=\"color: #a31515\">--strict<\/code> on, you can specify <code style=\"color: #a31515\">--strictFunctionTypes false<\/code> on the command line or in your <code style=\"color: #a31515\">tsconfig.json<\/code> as so:<\/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>strict<span class=\"pl-pds\">\"<\/span><\/span>: <span style=\"color: #0000ff\">true<\/span>,\n        <span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>strictFunctionTypes<span class=\"pl-pds\">\"<\/span><\/span>: <span style=\"color: #0000ff\">false<\/span>\n    }\n}<\/pre>\n<\/div>\n<h2> Error suppression comments with <code style=\"color: #a31515;font-size: 29px\">\/\/ @ts-ignore<\/code><\/h2>\n<p>Historically, we&#8217;ve avoided error suppression within TypeScript because most cases where our users asked for it could be solved through more accurate declaration files, or using a type assertion to <code style=\"color: #a31515\">any<\/code>. However, over time, we have seen two motivating examples: migrating from JavaScript to TypeScript, and overcoming type-checks that live in legacy code.<\/p>\n<p>For the former, the benefits are obvious: you want to start moving over to TypeScript, but you&#8217;ve run into a pattern that is particularly difficult to model. You could spend time trying to understand it, but maybe you&#8217;d rather get rid of it later on anyway, so you just want to avoid errors because your code will still run just fine.<\/p>\n<p>The latter situation is a bit less obvious. The motivation here was that within some large organizations, dependencies across projects are updated in tandem, including TypeScript itself and <code style=\"color: #a31515\">@types<\/code> declaration files from Definitely Typed. If any change introduces a type-checking error, someone has to fix that to avoid breaking the build. But now a question arises: who should that be on an unmaintained project? While the error is usually useful, the reality of the situation is that the code has functioned thus far and teams have finite resources.<\/p>\n<p>That&#8217;s why with TypeScript 2.6 we&#8217;re bringing <code style=\"color: #a31515\">\/\/ @ts-ignore<\/code> comments to TypeScript files. These comments suppress any errors that occur on the next line. For example, in the following code, TypeScript would ordinarily report an error about the <code style=\"color: #a31515\">console.log<\/code> statement being unreachable. In TypeScript 2.6, the <code style=\"color: #a31515\">\/\/ @ts-ignore<\/code> comment squelches the error entirely.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">if<\/span> (<span style=\"color: #0000ff\">false<\/span>) {\n    <span style=\"color: #148A14\">\/\/ @ts-ignore: Unreachable code error<\/span>\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}<\/pre>\n<\/div>\n<p>While this feature is motivated by pragmatism, we encourage taking type-checking errors seriously. Our guidance here is to use suppression comments <em>very sparingly<\/em>. In situations where you <em>do<\/em> need to use these comments, we suggest leaving a trailing explanation of why the comment is necessary like in the example above.<\/p>\n<h2> Revised tagged template emit<\/h2>\n<p>Tagged template strings have been supported and type-checked in TypeScript for quite some time now. Let&#8217;s take a simple example of a tagged template string:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">function<\/span> foo(<span class=\"pl-v\">arg<\/span><span class=\"pl-k\">:<\/span> <span style=\"color: #267F99\">TemplateStringsArray<\/span>) {\n    <span style=\"color: #0000ff\">return<\/span> <span class=\"pl-smi\">arg<\/span>;\n}\n\n<span style=\"color: #0000ff\">function<\/span> bar() {\n    <span style=\"color: #0000ff\">return<\/span> <span class=\"pl-smi\">foo<\/span> <span style=\"color: #a31515\"><span class=\"pl-pds\">`<\/span>hello world<span class=\"pl-pds\">`<\/span><\/span>;\n}<\/pre>\n<\/div>\n<p>When emitting to ES3\/ES5, that tagged template more or less boils down to calling <code style=\"color: #a31515\">foo<\/code> like so:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">function<\/span> foo(arg) {\n    <span style=\"color: #0000ff\">return<\/span> <span class=\"pl-smi\">arg<\/span>;\n}\n<span style=\"color: #0000ff\">function<\/span> bar() {\n    <span style=\"color: #0000ff\">var<\/span> _a <span class=\"pl-k\">=<\/span> [<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>hello world<span class=\"pl-pds\">\"<\/span><\/span>];\n    <span class=\"pl-smi\">_a<\/span>.<span class=\"pl-smi\">raw<\/span> <span class=\"pl-k\">=<\/span> [<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>hello world<span class=\"pl-pds\">\"<\/span><\/span>];\n    <span class=\"pl-en\">foo<\/span>(<span class=\"pl-smi\">_a<\/span>);\n}<\/pre>\n<\/div>\n<p>However, the ECMAScript specification says that every time <code style=\"color: #a31515\">foo `hello world`<\/code> gets invoked, <code style=\"color: #a31515\">foo<\/code> will get the same template object, so re-initializing <code style=\"color: #a31515\">_a<\/code> in <code style=\"color: #a31515\">bar<\/code> is incorrect. This is problematic because some libraries have an expectation that with the above example, any invocation of <code style=\"color: #a31515\">bar()<\/code> will return the same value.<\/p>\n<p>In TypeScript 2.6, within a module, tagged template string objects are now cached after the first invocation. This aligns more closely with recent revisions to the ECMAScript spec, and we will be following up with this behavior to ensure it correctly works within global code as well.<\/p>\n<p>This also means that TypeScript users can use libraries like <a href=\"https:\/\/github.com\/PolymerLabs\/lit-html\">lit-html<\/a> and <a href=\"https:\/\/github.com\/WebReflection\/hyperHTML\">hyperHTML<\/a> which take advantage of the new behavior.<\/p>\n<h2> Breaking changes and deprecations<\/h2>\n<p>There are several minor changes that may impact your codebase. While you can read up more about them in our <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/Breaking-Changes\">Breaking Changes<\/a> section, please keep these in mind when upgrading.<\/p>\n<ul>\n<li>Write only references are now considered unused under <code style=\"color: #a31515\">--noUnusedLocals<\/code> and <code style=\"color: #a31515\">--noUnusedParameters<\/code>.<\/li>\n<li>In ambient contexts (e.g. declaration files, and <code style=\"color: #a31515\">declare module<\/code> blocks), expressions are now disallowed in <code style=\"color: #a31515\">default<\/code>exports.<\/li>\n<li>Uninhabitable types resulting from intersections (<code style=\"color: #a31515\">number &amp; string<\/code>, <code style=\"color: #a31515\">\"foo\" &amp; 42<\/code>, etc.) will simplify to <code style=\"color: #a31515\">never<\/code> when placed in a union.<\/li>\n<li>Various changes have been made to DOM declarations in <code style=\"color: #a31515\">lib.d.ts<\/code>.<\/li>\n<\/ul>\n<p>As for deprecations, the <code style=\"color: #a31515\">getSymbolDisplayBuilder<\/code> API is now deprecated. For simple use cases, you should be able to move to <code style=\"color: #a31515\">TypeChecker#symbolToString<\/code>. For more advanced use cases, we plan to bring <code style=\"color: #a31515\">symbolToString<\/code> fully up to par with <code style=\"color: #a31515\">SymbolDisplayBuilder<\/code> in functionality by TypeScript 2.7. In a release following TypeScript 2.7, we will be be removing <code style=\"color: #a31515\">getSymbolDisplayBuilder<\/code>.<\/p>\n<h2> Other features and upcoming plans<\/h2>\n<p>We have a more complete description of new features in our <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What's-new-in-TypeScript\">What&#8217;s New in TypeScript<\/a> wiki page, and try to keep our <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/Roadmap\">Roadmap<\/a> up to date to provide users with an idea of what&#8217;s in store. For the full release, we anticipate we&#8217;ll have some more tooling-related features that you can take advantage of.<\/p>\n<p>Thanks for reading up on what we&#8217;ve got in store! We hope you enjoy this RC, and we&#8217;d love any and all feedback to ensure TypeScript 2.6 is a wonderful release.\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript 2.6 RC is now available! To get started with the latest stable version of TypeScript, you can grab it through NuGet, or use the following command with npm: npm install -g typescript@rc Visual Studio 2015 users (who have Update 3) can install TypeScript 2.6 RC from here, and Visual Studio 2017 users using version [&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-1197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>TypeScript 2.6 RC is now available! To get started with the latest stable version of TypeScript, you can grab it through NuGet, or use the following command with npm: npm install -g typescript@rc Visual Studio 2015 users (who have Update 3) can install TypeScript 2.6 RC from here, and Visual Studio 2017 users using version [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1197","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=1197"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1197\/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=1197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=1197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=1197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}