{"id":1145,"date":"2017-08-31T16:44:32","date_gmt":"2017-08-31T16:44:32","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/?p=1145"},"modified":"2019-02-20T10:45:48","modified_gmt":"2019-02-20T17:45:48","slug":"announcing-typescript-2-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-2-5\/","title":{"rendered":"Announcing TypeScript 2.5"},"content":{"rendered":"<div style=\"font-size: 16px\">\nToday we&#8217;re happy to bring you TypeScript 2.5! If you&#8217;ve read<span>\u00a0<\/span><a href=\"https:\/\/blogs.msdn.microsoft.com\/typescript\/2017\/08\/17\/announcing-typescript-2-5-rc\/\">our RC announcement<\/a>, we&#8217;ve got a few new items that we&#8217;re proud to highlight!<\/p>\n<p>If you&#8217;re not familiar with TypeScript, it&#8217;s a typed superset of JavaScript. More simply put, it&#8217;s just JavaScript with optional static types. Static types can make it easier to maintain your code by catching bugs early on, making it easier to navigate your projects, giving accurate code completion, and providing handy fixes for when you do make mistakes. By making types optional, you can get the flexibility of plain JavaScript when you need it, all while TypeScript also gives you the option to tighten things up to bring you more type safety. You can<span>\u00a0<\/span><a href=\"http:\/\/www.typescriptlang.org\/\">learn more about TypeScript on our website<\/a>.<\/p>\n<p>To start using TypeScript, you can grab it<span>\u00a0<\/span><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.TypeScript.MSBuild\">through NuGet<\/a><span>\u00a0<\/span>or use the following command with npm:<\/p>\n<div class=\"highlight highlight-source-shell\">\n<pre>npm install -g typescript<\/pre>\n<\/div>\n<p>Visual Studio 2015 users (who have<span>\u00a0<\/span><a href=\"https:\/\/www.visualstudio.com\/en-us\/news\/releasenotes\/vs2015-update3-vs\">Update 3<\/a>)<span>\u00a0<\/span><a href=\"http:\/\/download.microsoft.com\/download\/6\/D\/8\/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA\/2.5.2-TS-release-dev14update3-20170830.5\/TypeScript_Dev14Full.exe\">can install TypeScript 2.5 from here<\/a>, and Visual Studio 2017 users using<span>\u00a0<\/span><a href=\"https:\/\/www.visualstudio.com\/en-us\/news\/releasenotes\/vs2017-relnotes-v15.2\">version 15.2 or later<\/a><span>\u00a0<\/span>will be able to get TypeScript<span>\u00a0<\/span><a href=\"http:\/\/download.microsoft.com\/download\/7\/0\/A\/70A6AC0E-8934-4396-A43E-445059F430EA\/2.5.2-TS-release-dev14update3-20170830.5\/TypeScript_SDK.exe\">by simply installing it from here<\/a>. Visual Studio 2017 users should be sure to <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/Updating-TypeScript-in-Visual-Studio-2017\">read up on how you can configure your project to target specific versions of TypeScript<\/a>.<\/p>\n<p>While TypeScript 2.5 will be available for other editors soon, in the meantime you can configure<span>\u00a0<\/span><a href=\"https:\/\/code.visualstudio.com\/Docs\/languages\/typescript#_using-newer-typescript-versions\">Visual Studio Code<\/a><span>\u00a0<\/span>and<span>\u00a0<\/span><a href=\"https:\/\/github.com\/Microsoft\/TypeScript-Sublime-Plugin\/#note-using-different-versions-of-typescript\">Sublime Text<\/a><span>\u00a0<\/span>to use a newer version.<span>\u00a0<\/span><a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/TypeScript-Editor-Support\">Other editors<\/a><span>\u00a0<\/span>may have different approaches to getting TypeScript 2.5 running.<\/p>\n<p>Let&#8217;s look at what TypeScript 2.5 brings!<\/p>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/4f13fd3d6ad448c05fb25ab2d250d6cf#the-extract-function-and-extract-method-refactorings\" id=\"user-content-the-extract-function-and-extract-method-refactorings\" class=\"anchor\"><\/a>The<span>\u00a0<\/span><em>Extract Function<\/em><span>\u00a0<\/span>and<span>\u00a0<\/span><em>Extract Method<\/em><span>\u00a0<\/span>refactorings<\/h2>\n<p>Our team is always in search of ways to bring more powerful tools to the TypeScript and JavaScript world. That&#8217;s why with TypeScript 2.5 we&#8217;ve invested a lot into implementing<span>\u00a0<\/span><em>extract method<\/em><span>\u00a0<\/span>and<span>\u00a0<\/span><em>extract function<\/em>: two new refactorings that make complex rewrites trivial.<\/p>\n<p><video width=\"100%\" autoplay muted loop src=\"http:\/\/devblogs.microsoft.com\/typescript\/wp-content\/uploads\/sites\/11\/2017\/08\/boxBlur3.1.mp4\">\n    A series of extract function refactorings applied to a large unwieldy function with four nested for-loops.\n<\/video><\/p>\n<p>If you&#8217;re using Visual Studio Code, this refactoring will be available in the upcoming release (though you can try it now by<span>\u00a0<\/span><a href=\"https:\/\/code.visualstudio.com\/insiders\">using VS Code Insiders releases<\/a>).<\/p>\n<p>This feature is still fairly new so we expect that there will still be room for improvement, but we&#8217;re excited to hear your feedback so we can polish things out.<\/p>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/4f13fd3d6ad448c05fb25ab2d250d6cf#new-quick-fixes\" id=\"user-content-new-quick-fixes\" class=\"anchor\"><\/a>New quick fixes<\/h2>\n<p>We&#8217;ve also added a few quick fixes for when TypeScript can guess a little bit at what you meant to write.<\/p>\n<p>One new quick fix will get triggered when you try to use JSDoc-style types in TypeScript. If you&#8217;re in the habit of writing types like these, you might be surprised to find out that they&#8217;re not valid in TypeScript, but TypeScript is happy to push us in the right direction.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/typescript\/wp-content\/uploads\/sites\/11\/2017\/08\/accidentalJsdoc.gif\" alt=\"Quick fixes correcting JSDoc-style types to TypeScript-style types.\" \/><\/p>\n<p>We&#8217;ve also added a quick fix for when you try to reference the type of a property off of another type incorrectly. For example, for the following code<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">interface<\/span> <span style=\"color: #267F99\">Foo<\/span> {\n    bar<span class=\"pl-k\">:<\/span> <span style=\"color: #0000ff\">number<\/span>;\n}<\/pre>\n<\/div>\n<p>We might want to declare a variable named<span>\u00a0<\/span><code style=\"color: #a31515\">xyz<\/code><span>\u00a0<\/span>whose type is tied to to the type of<span>\u00a0<\/span><code style=\"color: #a31515\">bar<\/code>. The correct way to write this would be using an indexed access type:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #148A14\">\/\/ Get the type of the property named 'bar' off of 'Foo'.<\/span>\n<span style=\"color: #0000ff\">var<\/span> xyz<span class=\"pl-k\">:<\/span> <span style=\"color: #267F99\">Foo<\/span>[<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>bar<span class=\"pl-pds\">\"<\/span><\/span>]<\/pre>\n<\/div>\n<p>but we might accidentally write<span>\u00a0<\/span><code style=\"color: #a31515\">var xyz: Foo.bar<\/code>. TypeScript now can now suggest the correct one in many cases.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/typescript\/wp-content\/uploads\/sites\/11\/2017\/08\/propReference.gif\" alt=\"A quick fix that corrects `Foo.bar` to `Foo['bar']`.\" \/><\/p>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/4f13fd3d6ad448c05fb25ab2d250d6cf#jsdoc-type-assertion-support-in-javascript-files\" id=\"user-content-jsdoc-type-assertion-support-in-javascript-files\" class=\"anchor\"><\/a>JSDoc type assertion support in JavaScript files<\/h2>\n<p>In TypeScript 2.4, we introduced the ability to get type-checking in JavaScript files so that users can more easily migrate to TypeScript, and have an easier experience with certain more lightweight projects. Taking advantage of this is as simple as adding a<span>\u00a0<\/span><code style=\"color: #a31515\">\/\/ @ts-check<\/code><span>\u00a0<\/span>at the top of your<span>\u00a0<\/span><code style=\"color: #a31515\">.js<\/code><span>\u00a0<\/span>file, or turning on the<span>\u00a0<\/span><code style=\"color: #a31515\">checkJs<\/code><span>\u00a0<\/span>flag in yout<span>\u00a0<\/span><code style=\"color: #a31515\">tsconfig.json<\/code>&#8216;s<span>\u00a0<\/span><code style=\"color: #a31515\">compilerOptions<\/code>.<\/p>\n<p>One thing that it lacked was the ability to &#8220;cast&#8221; or &#8220;assert&#8221; the type of an expression. This is important for situations where you know a little more than the type-checker and need to tell it so. To come up with a trivial example, let&#8217;s take the following JavaScript code:<\/p>\n<div class=\"highlight highlight-source-js\">\n<pre><span style=\"color: #148A14\">\/\/ @ts-check<\/span>\n\n<span style=\"color: #0000ff\">var<\/span> foo <span class=\"pl-k\">=<\/span> <span>Math<\/span>.<span>random<\/span>() <span class=\"pl-k\">?<\/span> <span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>hello<span class=\"pl-pds\">\"<\/span><\/span> <span class=\"pl-k\">:<\/span> <span style=\"color: #09885A\">100<\/span>;\n\n<span class=\"pl-smi\">foo<\/span>.<span>toUpperCase<\/span>();\n<span style=\"color: #148A14\">\/\/  ~~~~~~~~~~~<\/span>\n<span style=\"color: #148A14\">\/\/  Error! Property 'toUpperCase' does not exist on type 'string | number'.<\/span><\/pre>\n<\/div>\n<p>TypeScript correctly indicates that we might be calling a method that doesn&#8217;t exist on<span>\u00a0<\/span><code style=\"color: #a31515\">number<\/code>s. If we wanted to get around this so we can easily get our runtime error, we could write a JSDoc type assertion:<\/p>\n<div class=\"highlight highlight-source-js\">\n<pre><span style=\"color: #148A14\">\/\/ Works!<\/span>\n<span style=\"color: #0000ff\">var<\/span> bar <span class=\"pl-k\">=<\/span> <span style=\"color: #148A14\">\/** <span class=\"pl-k\">@type<\/span> <span class=\"pl-en\">{string}<\/span> *\/<\/span> (foo);\n<span class=\"pl-smi\">bar<\/span>.<span>toUpperCase<\/span>();<\/pre>\n<\/div>\n<p>The syntax is<span>\u00a0<\/span><code style=\"color: #a31515\">\/** @type {YOUR_TYPE_HERE} *\/ (someParenthesizedExpression)<\/code>.<\/p>\n<p>Keep in mind that if you&#8217;ve enabled JavaScript checking on a file, invalid type assertions will still get caught:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">var<\/span> clearlyNumber <span class=\"pl-k\">=<\/span> <span style=\"color: #148A14\">\/** <span class=\"pl-k\">@type<\/span> <span class=\"pl-en\">{string}<\/span> *\/<\/span> (<span style=\"color: #09885A\">100<\/span>);\n<span style=\"color: #148A14\">\/\/                      ~~~~~~~~~~~~~~<\/span>\n<span style=\"color: #148A14\">\/\/ Error! Type 'number' cannot be converted to type 'string'.<\/span><\/pre>\n<\/div>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/4f13fd3d6ad448c05fb25ab2d250d6cf#optional-catch-clauses\" id=\"user-content-optional-catch-clauses\" class=\"anchor\"><\/a>Optional<span>\u00a0<\/span><code style=\"color: #a31515;font-size: 29px\">catch<\/code><span>\u00a0<\/span>clauses<\/h2>\n<p>Thanks to work by<span>\u00a0<\/span><a href=\"https:\/\/github.com\/tinganho\">Tingan Ho<\/a>, TypeScript 2.5 brings a new ECMAScript-bound feature for making catch clauses optional. Much of the time, you&#8217;ll find yourself writing a<span>\u00a0<\/span><code style=\"color: #a31515\">try<\/code>\/<code style=\"color: #a31515\">catch<\/code><span>\u00a0<\/span>but not really caring about the thrown error. For example:<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">let<\/span> contents;\n<span style=\"color: #0000ff\">try<\/span> {\n    <span class=\"pl-smi\">contents<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">fs<\/span>.<span class=\"pl-en\">readFileSync<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>.config_file<span class=\"pl-pds\">\"<\/span><\/span>).<span>toString<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">'<\/span>utf8<span class=\"pl-pds\">'<\/span><\/span>);\n}\n<span style=\"color: #0000ff\">catch<\/span> (<span class=\"pl-smi\">unusedError<\/span>) {\n    <span style=\"color: #148A14\">\/\/ File might not exist, just fall back to some defaults.<\/span>\n    <span class=\"pl-smi\">contents<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-en\">createDefaultContents<\/span>();\n}<\/pre>\n<\/div>\n<p>Notice that<span>\u00a0<\/span><code style=\"color: #a31515\">unusedError<\/code><span>\u00a0<\/span>is never referenced in the above example. Barring philosophical issues about whether it&#8217;s appropriate to ignore the error, we can make our code a little cleaner by taking advantage of the fact that the<span>\u00a0<\/span><code style=\"color: #a31515\">catch<\/code><span>\u00a0<\/span>variable is now optional.<\/p>\n<div class=\"highlight highlight-source-ts\">\n<pre><span style=\"color: #0000ff\">let<\/span> contents;\n<span style=\"color: #0000ff\">try<\/span> {\n    <span class=\"pl-smi\">contents<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">fs<\/span>.<span class=\"pl-en\">readFileSync<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">\"<\/span>.config_file<span class=\"pl-pds\">\"<\/span><\/span>).<span>toString<\/span>(<span style=\"color: #a31515\"><span class=\"pl-pds\">'<\/span>utf8<span class=\"pl-pds\">'<\/span><\/span>);\n}\n<span style=\"color: #0000ff\">catch<\/span> {\n    <span style=\"color: #148A14\">\/\/ File might not exist, just fall back to some defaults.<\/span>\n    <span class=\"pl-smi\">contents<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-en\">createDefaultContents<\/span>();\n}<\/pre>\n<\/div>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/4f13fd3d6ad448c05fb25ab2d250d6cf#deduplicated-and-redirected-packages\" id=\"user-content-deduplicated-and-redirected-packages\" class=\"anchor\"><\/a>Deduplicated and redirected packages<\/h2>\n<p>When importing using the<span>\u00a0<\/span><code style=\"color: #a31515\">Node<\/code><span>\u00a0<\/span>module resolution strategy in TypeScript 2.5, the compiler will now check whether files originate from &#8220;identical&#8221; packages. If a file originates from a package with a<span>\u00a0<\/span><code style=\"color: #a31515\">package.json<\/code><span>\u00a0<\/span>containing the same<span>\u00a0<\/span><code style=\"color: #a31515\">name<\/code><span>\u00a0<\/span>and<span>\u00a0<\/span><code style=\"color: #a31515\">version<\/code><span>\u00a0<\/span>fields as a previously encountered package, then TypeScript will redirect itself to the top-most package. This helps resolve problems where two packages might contain identical declarations of classes, but which contain<span>\u00a0<\/span><code style=\"color: #a31515\">private<\/code><span>\u00a0<\/span>members that cause them to be structurally incompatible.<\/p>\n<p>As a nice bonus, this can also reduce the memory and runtime footprint of the compiler and language service by avoiding loading<span>\u00a0<\/span><code style=\"color: #a31515\">.d.ts<\/code><span>\u00a0<\/span>files from duplicate packages.<\/p>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/4f13fd3d6ad448c05fb25ab2d250d6cf#the---preservesymlinks-compiler-flag\" id=\"user-content-the---preservesymlinks-compiler-flag\" class=\"anchor\"><\/a>The<span>\u00a0<\/span><code style=\"color: #a31515;font-size: 29px\">--preserveSymlinks<\/code><span>\u00a0<\/span>compiler flag<\/h2>\n<p>TypeScript 2.5 brings the<span>\u00a0<\/span><code style=\"color: #a31515\">preserveSymlinks<\/code><span>\u00a0<\/span>flag, which parallels the behavior of<span>\u00a0<\/span><a href=\"https:\/\/nodejs.org\/api\/cli.html#cli_preserve_symlinks\">the<span>\u00a0<\/span><code style=\"color: #a31515\">--preserve-symlinks<\/code><span>\u00a0<\/span>flag in Node.js<\/a>. This flag also exhibits the opposite behavior to Webpack&#8217;s<span>\u00a0<\/span><code style=\"color: #a31515\">resolve.symlinks<\/code><span>\u00a0<\/span>option (i.e. setting TypeScript&#8217;s<span>\u00a0<\/span><code style=\"color: #a31515\">preserveSymlinks<\/code><span>\u00a0<\/span>to<span>\u00a0<\/span><code style=\"color: #a31515\">true<\/code><span>\u00a0<\/span>parallels setting Webpack&#8217;s<span>\u00a0<\/span><code style=\"color: #a31515\">resolve.symlinks<\/code><span>\u00a0<\/span>to<span>\u00a0<\/span><code style=\"color: #a31515\">false<\/code>, and vice-versa).<\/p>\n<p>In this mode, references to modules and packages (e.g.<span>\u00a0<\/span><code style=\"color: #a31515\">import<\/code>s and<span>\u00a0<\/span><code style=\"color: #a31515\">\/\/\/ &lt;reference type=\"...\" \/&gt;<\/code><span>\u00a0<\/span>directives) are all resolved relative to the location of the symbolic link file, rather than relative to the path that the symbolic link resolves to. For a more concrete example, we&#8217;ll defer to<span>\u00a0<\/span><a href=\"https:\/\/nodejs.org\/api\/cli.html#cli_preserve_symlinks\">the documentation on the Node.js website<\/a>.<\/p>\n<h2><a href=\"https:\/\/gist.github.com\/DanielRosenwasser\/4f13fd3d6ad448c05fb25ab2d250d6cf#enjoy\" id=\"user-content-enjoy\" class=\"anchor\"><\/a>Enjoy!<\/h2>\n<p>We hope our work in TypeScript 2.5 will make you happier and more productive. If it did, let us know on Twitter with the<span>\u00a0<\/span><a href=\"https:\/\/twitter.com\/intent\/tweet?text=%23iHeartTypeScript\">#iHeartTypeScript<\/a><span>\u00a0<\/span>hashtag.<\/p>\n<p>Out team appreciates any sort of feedback on how we can improve. Feel free to let us know about any issues you run into or helpful ideas you think might make TypeScript even better to use<span>\u00a0<\/span><a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/issues\">on our GitHub issue tracker<\/a>.<\/p>\n<p>As for what&#8217;s next, we&#8217;ve caught most of what&#8217;s new on this blog post, but you can always check out our<span>\u00a0<\/span><a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What's-new-in-TypeScript\">what&#8217;s new in TypeScript<\/a><span>\u00a0<\/span>page on our wiki for some more details, and keep an eye on<span>\u00a0<\/span><a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/Roadmap\">our Roadmap<\/a><span>\u00a0<\/span>that highlights our current plans and is frequently updated.<\/p>\n<p>Thanks for reading and happy hacking!\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re happy to bring you TypeScript 2.5! If you&#8217;ve read\u00a0our RC announcement, we&#8217;ve got a few new items that we&#8217;re proud to highlight! If you&#8217;re not familiar with TypeScript, it&#8217;s a typed superset of JavaScript. More simply put, it&#8217;s just JavaScript with optional static types. Static types can make it easier to maintain 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-1145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>Today we&#8217;re happy to bring you TypeScript 2.5! If you&#8217;ve read\u00a0our RC announcement, we&#8217;ve got a few new items that we&#8217;re proud to highlight! If you&#8217;re not familiar with TypeScript, it&#8217;s a typed superset of JavaScript. More simply put, it&#8217;s just JavaScript with optional static types. Static types can make it easier to maintain your [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1145","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=1145"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1145\/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=1145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=1145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=1145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}