{"id":582,"date":"2016-02-22T21:31:29","date_gmt":"2016-02-22T21:31:29","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/?p=582"},"modified":"2019-02-20T10:46:06","modified_gmt":"2019-02-20T17:46:06","slug":"announcing-typescript-1-8-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-1-8-2\/","title":{"rendered":"Announcing TypeScript 1.8"},"content":{"rendered":"<p>Today we are thrilled to announce the release of TypeScript 1.8. In the <a href=\"https:\/\/blogs.msdn.microsoft.com\/typescript\/2016\/01\/28\/announcing-typescript-1-8-beta\/\" target=\"_blank\">TypeScript 1.8 Beta release blog post<\/a>, we highlighted some of the key features that are now available to TypeScript users &#8211; how <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What%27s-new-in-TypeScript#including-js-files-with---allowjs\">JavaScript in TypeScript compilation<\/a> provides a great way forward to start converting your JavaScript projects to TypeScript, <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What%27s-new-in-TypeScript#custom-jsx-factories-using---reactnamespace\">improvements<\/a> to <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What%27s-new-in-TypeScript#stateless-function-components-in-react\">JSX\/TSX support<\/a>, and our move to <a href=\"https:\/\/github.com\/Microsoft\/Chakracore\">ChakraCore<\/a>. This post supplements the above and details how TypeScript 1.8 provides full support for module augmentation, delivers a stronger type system with string literal types, and catches even more common bugs with smarter control flow analysis.<\/p>\n<p>TypeScript 1.8 is available for <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=48593\">Visual Studio 2015<\/a>, <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=48739\">Visual Studio 2013<\/a>, NuGet (<a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.TypeScript.Compiler\/\">Compiler<\/a> and <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.TypeScript.MSBuild\/\">MSBuild task<\/a>), <a href=\"https:\/\/www.npmjs.com\/package\/typescript\">npm<\/a>, and straight from the <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/releases\">source<\/a>.<\/p>\n<h2>Module Augmentation<\/h2>\n<p>TypeScript 1.8 allows users to design more modular libraries via module augmentation. This allows library authors to distribute their libraries in a piecemeal fashion. Previously, TypeScript made the assumption that modules wouldn&#8217;t change. With module augmentation, users have the ability to extend existing modules such that consumers can specify if they want to import the whole module or just a subset. This can be accomplished by simply adding an ambient module declaration and extending any existing types.<\/p>\n<pre><span style=\"color: #008000\">\/\/ scale.ts<\/span>\n<span style=\"color: #0000ff\">export class<\/span> Scale {\n    weightOnEarth(mass) {}\n}\n<span style=\"color: #008000\">\/\/ advancedScale.ts<\/span>\n<span style=\"color: #0000ff\">import<\/span> { Scale } <span style=\"color: #0000ff\">from<\/span> <span style=\"color: #a31515\">\".\/scale\"<\/span> ;<\/pre>\n<pre><span style=\"color: #008000\">\/\/ create augmentation for Scale<\/span>\n<span style=\"color: #0000ff\">declare module<\/span> <span style=\"color: #a31515\">\".\/scale\"<\/span> {\n    <span style=\"color: #008000\">\/\/ Augment Core class via interface merging<\/span>\n    <span style=\"color: #0000ff\">interface<\/span> Scale {\n        weightOnMoon(mass); <span style=\"color: #008000\">\/\/ not everyone needs moon weight<\/span>\n    }\n}\nScale.prototype.weightOnMoon = <span style=\"color: #008000\">\/* insert implementation *\/<\/span>;\n\n<span style=\"color: #008000\">\/\/ consumer.ts<\/span>\n<span style=\"color: #0000ff\">import<\/span> { Scale } <span style=\"color: #0000ff\">from<\/span> <span style=\"color: #a31515\">\".\/scale\"<\/span>;\n<span style=\"color: #0000ff\">import<\/span> <span style=\"color: #a31515\">\".\/advancedScale\"<\/span>;\n\n<span style=\"color: #0000ff\">let<\/span> scale: Scale;\nscale.weightOnMoon(10);  <span style=\"color: #008000\">\/\/ ok<\/span><\/pre>\n<h2>String Literal Types<\/h2>\n<p>It is very common for JavaScript libraries to consume a string as a configuration parameter, and usually in such cases you want to restrict the possible strings to a certain set. Take the following example of a UI element that can specify an easing method:<\/p>\n<pre><span style=\"color: #0000ff\">declare class<\/span> UIElement {\n    animate(options: AnimationOptions): <span style=\"color: #0000ff\">void<\/span>;\n}\n\n<span style=\"color: #0000ff\">interface<\/span> AnimationOptions {\n    deltaX: <span style=\"color: #0000ff\">number<\/span>;\n    deltaY: <span style=\"color: #0000ff\">number<\/span>;\n    easing: <span style=\"color: #0000ff\">string<\/span>;  <span style=\"color: #008000\">\/\/ Can be \"ease-in\", \"ease-out\", \"ease-in-out\"<\/span>\n}\n<span style=\"color: #0000ff\">new<\/span> UIElement().animate({ deltaX: 100, deltaY: 100, easing: <span style=\"color: #a31515\">\"out\"<\/span> }); <span style=\"color: #008000\">\/\/ No error<\/span><\/pre>\n<p>With regular strings, there is no protection from this class of error. However, starting with TypeScript 1.8, strings in a type position will become <em>string literal types<\/em>. Only exact string matches are assignable to string literal types, and like any other type, they can be used in union types as well. So if we rewrite the <code>AnimationOptions<\/code> interface with string literal types, the API users now get type protection:<\/p>\n<pre><span style=\"color: #0000ff\">interface<\/span> AnimationOptions {\n    deltaX: <span style=\"color: #0000ff\">number<\/span>;\n    deltaY: <span style=\"color: #0000ff\">number<\/span>;\n    easing: <span style=\"color: #a31515\">\"ease-in\"<\/span> | <span style=\"color: #a31515\">\"ease-out\"<\/span> | <span style=\"color: #a31515\">\"ease-in-out\"<\/span>;\n}\n\n<span style=\"color: #008000\">\/\/ Error: Type '\"out\"' is not assignable to type '\"ease-in\" | \"ease-out\" | \"ease-in-out\"'<\/span>\n<span style=\"color: #0000ff\">new<\/span> UIElement().animate({ deltaX: 100, deltaY: 100, easing: <span style=\"color: #a31515\">\"out\"<\/span> });<\/pre>\n<h2>Smarter Control Flow Analysis<\/h2>\n<p>It is common for complex logic that results in many branching code paths to produce hard to find bugs. TypeScript 1.8 delivers better control flow analysis to help you catch these at compile time.<\/p>\n<h3>Unreachable Code<\/h3>\n<p>JavaScript&#8217;s automatic semicolon insertion is fairly useful, but it can also lead to unwanted results at times. The example below, which actually returns <code>undefined<\/code>, highlights a common bug that is now detected by the TypeScript compiler.<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span> importantData() {\n    <span style=\"color: #0000ff\">return<\/span>          <span style=\"color: #008000\">\/\/ Automatic semicolon insertion triggered with newline<\/span>\n    {\n        x: <span style=\"color: #a31515\">\"thing\"<\/span>  <span style=\"color: #008000\">\/\/ Error: Unreachable code detected.<\/span>\n    }\n}<\/pre>\n<p>If for whatever reason you are experiencing too many false positives, <a href=\"http:\/\/github.com\/microsoft\/typescript\/issues\" target=\"_blank\">please let us know<\/a>, but you can toggle this feature off with the <code>--allowUnreachableCode<\/code> flag.<\/p>\n<h3>Implicit Returns<\/h3>\n<p>In JavaScript, reaching the end of a function with no return statement results in implicitly returning <code>undefined<\/code>. Sometimes this is the intended behavior, but often times it can be a red flag for gaps in complex logic. Unlike unreachable code, this check is <strong>off<\/strong> by default. To get the added safety, just add the <code>--noImplicitReturns<\/code> flag.<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span> isPizza(food) {   <span style=\"color: #008000\">\/\/ Error: Not all code paths return a value.<\/span>\n    <span style=\"color: #0000ff\">if<\/span> (food === <span style=\"color: #a31515\">\"pizza\"<\/span>) {\n        <span style=\"color: #0000ff\">return<\/span> <span style=\"color: #0000ff\">true<\/span>;\n    }\n    <span style=\"color: #0000ff\">else<\/span> <span style=\"color: #0000ff\">if<\/span> (food === <span style=\"color: #a31515\">\"pie\"<\/span>) {\n        <span style=\"color: #0000ff\">return<\/span> <span style=\"color: #0000ff\">true<\/span>;\n    }\n    <span style=\"color: #008000\">\/\/ implicitly returns `undefined`<\/span>\n}<\/pre>\n<p>TypeScript 1.8 also includes control logic for unused labels and case clause fall-through. You can read more about those <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What%27s-new-in-TypeScript#control-flow-analysis-errors\" target=\"_blank\">here<\/a>.<\/p>\n<h2>So Much More!<\/h2>\n<p>If you want the deep dive on all the new features released with 1.8, check out our <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/What%27s-new-in-TypeScript#typescript-18\" target=\"_blank\">what&#8217;s new page<\/a>. As always, we encourage you to weigh in on the future of TypeScript by browsing our <a href=\"https:\/\/github.com\/microsoft\/typescript\/issues\" target=\"_blank\">existing issues<\/a>, throwing us a <a href=\"https:\/\/github.com\/microsoft\/typescript\/pulls\" target=\"_blank\">pull request<\/a>, or just hanging out with the team on <a href=\"https:\/\/gitter.im\/microsoft\/typescript\" target=\"_blank\">gitter<\/a>.<\/p>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we are thrilled to announce the release of TypeScript 1.8. In the TypeScript 1.8 Beta release blog post, we highlighted some of the key features that are now available to TypeScript users &#8211; how JavaScript in TypeScript compilation provides a great way forward to start converting your JavaScript projects to TypeScript, improvements to JSX\/TSX [&hellip;]<\/p>\n","protected":false},"author":380,"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-582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>Today we are thrilled to announce the release of TypeScript 1.8. In the TypeScript 1.8 Beta release blog post, we highlighted some of the key features that are now available to TypeScript users &#8211; how JavaScript in TypeScript compilation provides a great way forward to start converting your JavaScript projects to TypeScript, improvements to JSX\/TSX [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/582","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\/380"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/comments?post=582"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/582\/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=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}