{"id":1824,"date":"2015-01-16T10:30:00","date_gmt":"2015-01-16T10:30:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/typescript\/2015\/01\/16\/announcing-typescript-1-4\/"},"modified":"2019-02-20T10:46:21","modified_gmt":"2019-02-20T17:46:21","slug":"announcing-typescript-1-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-1-4\/","title":{"rendered":"Announcing TypeScript 1.4"},"content":{"rendered":"<p>Today we&#8217;re happy to announce TypeScript 1.4. With TypeScript 1.4, we&#8217;ve continued to build new features that help you work with more JavaScript patterns, create richer typings, and use new ES6 features.<\/p>\n<p>You can try these improvements out as part of <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=400496\">Visual Studio 2015 CTP5<\/a>, <a href=\"https:\/\/visualstudiogallery.msdn.microsoft.com\/2d42d8dc-e085-45eb-a30b-3f7d50d55304\">Visual Studio 2013<\/a>, <a href=\"https:\/\/www.npmjs.com\/package\/typescript\">NPM<\/a>, and as <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\">source<\/a>.<\/p>\n<h2>Type System Improvements<\/h2>\n<h2>Union Types<\/h2>\n<p>JavaScript functions may take a number of possible argument types. Up to now, we&rsquo;ve supported this using function overloads. Starting with TypeScript 1.4, we&rsquo;ve generalized this capability and now allow you to specify that that a value is one of a number of different types using a union type:<\/p>\n<pre><span style=\"color: #0603d8\">function<\/span> <span style=\"color: black\">f<\/span>(<span class=\"pl-vpf\">x<\/span>: <span style=\"color: #0603d8\">number<\/span> | <span style=\"color: #0603d8\">number<\/span>[]) {\n  <span style=\"color: #0603d8\">if<\/span> (<span style=\"color: #0603d8\">typeof<\/span> x <span style=\"color: #0603d8\">===<\/span> <span style=\"color: #901319\"><span style=\"color: #901319\">\"<\/span>number<span style=\"color: #901319\">\"<\/span><\/span>) {\n    <span style=\"color: #0603d8\">return<\/span> x <span style=\"color: #0603d8\">+<\/span> <span class=\"pl-c1\">10<\/span>;\n  }\n  <span style=\"color: #0603d8\">else<\/span> {\n    <span style=\"color: #006a0a\">\/\/ return sum of numbers<\/span>\n  }\n}<\/pre>\n<p>Once you have a value of a union type, you can use a typeof and instanceof checks to use the value in a type-safe way. You&#8217;ll notice we use this in the above example and can treat x as a number type inside of the if-block.<\/p>\n<p>Union types are a new kind of type and work any place you specify a type.<\/p>\n<h2>Type Aliases<\/h2>\n<p>You can now define an alias for a type using the type keyword:<\/p>\n<pre><span style=\"color: #0000ff\">type<\/span> PrimitiveArray <span style=\"color: #0603d8\">=<\/span> <span class=\"pl-s3\">Array<\/span><span style=\"color: #0603d8\">&lt;<\/span><span style=\"color: #0603d8\">string<\/span>|<span style=\"color: #0603d8\">number<\/span>|<span style=\"color: #0603d8\">boolean<\/span><span style=\"color: #0603d8\">&gt;<\/span>;\n<span style=\"color: #0000ff\">type<\/span> MyNumber <span style=\"color: #0603d8\">=<\/span> <span style=\"color: #0603d8\">number<\/span>;\n<span style=\"color: #0000ff\">type<\/span> NgScope <span style=\"color: #0603d8\">=<\/span> ng.IScope;\n<span style=\"color: #0000ff\">type<\/span> Callback <span style=\"color: #0603d8\">=<\/span> () <span style=\"color: #0603d8\">=&gt;<\/span> <span style=\"color: #0603d8\">void<\/span>;<\/pre>\n<p>Type aliases are exactly the same as their original types; they are simply alternative names. You can use these aliases to better document your code and aid readability.<\/p>\n<h2>Const Enums<\/h2>\n<p>For heavy uses of enums, it&rsquo;s helpful to have an even more restricted form that we know is safe to always inline. This helps with performance, code size, and with working with cases where the enum aliases themselves may not be exported. Starting with TypeScript 1.4, you&rsquo;ll be able to make const enums.<\/p>\n<pre><span class=\"pl-s\" style=\"color: #0000ff\">const <\/span><span style=\"color: #0603d8\">enum<\/span> Color { Blue, Green, Red };\n<span style=\"color: #0603d8\">var<\/span> c <span style=\"color: #0603d8\">=<\/span> Color.Blue;<\/pre>\n<p>When compiled, the const enum is removed, leaving &lsquo;var c = 0 \/* Blue *\/&rsquo;.<\/p>\n<h2>And more&hellip;<\/h2>\n<p>You can get also more information and examples about updates to the type system on our <a href=\"http:\/\/blogs.msdn.com\/b\/typescript\/archive\/2014\/11\/18\/what-s-new-in-the-typescript-type-system.aspx\">TypeScript 1.4 sneak peek blog post<\/a>.<\/p>\n<h2>ECMAScript 6 Support<\/h2>\n<p>In addition to the type system improvements, one of the main goals for the upcoming TypeScript 2.0 release is to fully support the ECMAScript 6 standard. With TypeScript 1.4, we take another step towards this goal. In this release, we&rsquo;ve added a new ES6 output mode, support for let and const, and support for ES6 template strings.<\/p>\n<h2>ES6 output mode<\/h2>\n<p>In previous TypeScript releases, we included a &lsquo;&#8211;target&#8217; commandline option that allows you to choose between ES3 and ES5 output modes. We&rsquo;ve added a new &lsquo;ES6&rsquo; option to the targets commandline option:<\/p>\n<pre><code>&gt; tsc --target ES6 myfile.js <\/code><\/pre>\n<p>This option will be required for ES6 features that cannot be output to ES3 or ES5, just as ES5-only features, such as getters and setters, have required the ES5 target option.<\/p>\n<p>We&rsquo;re in the process of updating the existing language features from ES6, such as lambdas and classes, to support ES6 mode by outputting their native forms.&nbsp; You may need to install an <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/issues\/1696\">additional .d.ts file<\/a> for this to work.<\/p>\n<h2>Let\/Const<\/h2>\n<p>The ES6 &lsquo;let&rsquo; feature is similar to &lsquo;var&rsquo;, but it aims to simplify the mental model for the variable&rsquo;s scope. With &lsquo;let&rsquo;, you can scope variables to blocks of code rather than whole functions. For example:<\/p>\n<pre><span style=\"color: #0603d8\">function<\/span> <span style=\"color: black\">f<\/span>() {\n  <span style=\"color: #0000ff\">let<\/span> total <span style=\"color: #0603d8\">=<\/span> <span class=\"pl-c1\">0<\/span>;\n  <span style=\"color: #0000ff\">let<\/span> x <span style=\"color: #0603d8\">=<\/span> <span class=\"pl-c1\">5<\/span>;\n  <span style=\"color: #0603d8\">for<\/span> (let x <span style=\"color: #0603d8\">=<\/span> <span class=\"pl-c1\">1<\/span>; x <span style=\"color: #0603d8\">&lt;<\/span> <span class=\"pl-c1\">10<\/span>; x<span style=\"color: #0603d8\">++<\/span>) {\n    total <span style=\"color: #0603d8\">+=<\/span> x;\n  }\n  <span style=\"color: black\">console<\/span>.<span class=\"pl-s3\">log<\/span>(x);\n}\n\nf(); <span style=\"color: #006a0a\">\/\/ outputs 5<\/span><\/pre>\n<p>Notice how the two &lsquo;let x&rsquo; statements do not conflict. This is because the one used in the loop is in a different scope than the one outside of the loop. If we re-wrote this using vars, all &lsquo;x&rsquo; vars effectively combine into one, leading to rather confusing output.<\/p>\n<pre><span style=\"color: #0603d8\">function<\/span> <span style=\"color: black\">f<\/span>() {\n  <span style=\"color: #0603d8\">var<\/span> total <span style=\"color: #0603d8\">=<\/span> <span class=\"pl-c1\">0<\/span>;\n  <span style=\"color: #0603d8\">var<\/span> x <span style=\"color: #0603d8\">=<\/span> <span class=\"pl-c1\">5<\/span>;\n  <span style=\"color: #0603d8\">for<\/span> (<span style=\"color: #0603d8\">var<\/span> x <span style=\"color: #0603d8\">=<\/span> <span class=\"pl-c1\">1<\/span>; x <span style=\"color: #0603d8\">&lt;<\/span> <span class=\"pl-c1\">10<\/span>; x<span style=\"color: #0603d8\">++<\/span>) {\n    total <span style=\"color: #0603d8\">+=<\/span> x;\n  }\n  <span style=\"color: black\">console<\/span>.<span class=\"pl-s3\">log<\/span>(x);\n}\n\nf(); <span style=\"color: #006a0a\">\/\/ outputs 10<\/span><\/pre>\n<p>TypeScript now supports using &lsquo;let&rsquo; and &lsquo;const&rsquo; in addition to &lsquo;var&rsquo;. These currently require the ES6 output mode, but we&rsquo;re are investigating relaxing this restriction in future versions.<\/p>\n<h2>Template Strings<\/h2>\n<p>ECMAScript 6 has further improved on string interpolation in JavaScript by adding template strings. These special strings can freely mix in expressions, allowing a lighter syntax when pieces of a string depend on associated values:<\/p>\n<pre><span style=\"color: #0603d8\">var<\/span> rectangle <span style=\"color: #0603d8\">=<\/span> { height: <span class=\"pl-c1\">20<\/span>, width: <span class=\"pl-c1\">10<\/span> };\n<span style=\"color: #0603d8\">var<\/span> areaMessage <span style=\"color: #0603d8\">=<\/span> <span style=\"color: #800000\">`Rectangle area is <\/span><span style=\"color: #0603d8\">$<\/span>{rectangle.<span class=\"pl-sc\">height<\/span> <span style=\"color: #0603d8\">*<\/span> rectangle.<span class=\"pl-sc\">width<\/span>}<span style=\"color: #800000\">`<\/span>;<\/pre>\n<p>With the 1.4 release, TypeScript now supports ES6 template strings and can also compile them down to ES3\/ES5 expressions.<\/p>\n<h2>Looking Ahead<\/h2>\n<p>We&rsquo;re excited to bring new features into TypeScript, including more ECMAScript 6 features and our work on <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/issues\/1664\">async\/await<\/a>.&nbsp; As always, we&rsquo;d love to hear your feedback. You can get involved by trying out the release, sending us a <a href=\"https:\/\/github.com\/microsoft\/typescript\/issues\">bug<\/a>, playing with the <a href=\"https:\/\/github.com\/microsoft\/typescript\">source<\/a>, and sending us a <a href=\"https:\/\/github.com\/microsoft\/typescript\/pulls\">pull request<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re happy to announce TypeScript 1.4. With TypeScript 1.4, we&#8217;ve continued to build new features that help you work with more JavaScript patterns, create richer typings, and use new ES6 features. You can try these improvements out as part of Visual Studio 2015 CTP5, Visual Studio 2013, NPM, and as source. Type System Improvements [&hellip;]<\/p>\n","protected":false},"author":377,"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-1824","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>Today we&#8217;re happy to announce TypeScript 1.4. With TypeScript 1.4, we&#8217;ve continued to build new features that help you work with more JavaScript patterns, create richer typings, and use new ES6 features. You can try these improvements out as part of Visual Studio 2015 CTP5, Visual Studio 2013, NPM, and as source. Type System Improvements [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1824","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\/377"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/comments?post=1824"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/1824\/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=1824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=1824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=1824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}