Today we're happy to announce TypeScript 1.4. With TypeScript 1.4, we'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
Union Types
JavaScript functions may take a number of possible argument types. Up to now, we’ve supported this using function overloads. Starting with TypeScript 1.4, we’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:
functionf(x: number | number[]) {if (typeof x ==="number") {return x +10; }else {// return sum of numbers } }
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'll notice we use this in the above example and can treat x as a number type inside of the if-block.
Union types are a new kind of type and work any place you specify a type.
Type Aliases
You can now define an alias for a type using the type keyword:
type PrimitiveArray =Array<string|number|boolean>;type MyNumber =number;type NgScope = ng.IScope;type Callback = () =>void;
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.
Const Enums
For heavy uses of enums, it’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’ll be able to make const enums.
const enum Color { Blue, Green, Red };var c = Color.Red;
When compiled, the const enum is removed, leaving ‘var c = 0 /* Blue */’.
And more…
You can get also more information and examples about updates to the type system on our TypeScript 1.4 sneak peek blog post.
ECMAScript 6 Support
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’ve added a new ES6 output mode, support for let and const, and support for ES6 template strings.
ES6 output mode
In previous TypeScript releases, we included a ‘--target' commandline option that allows you to choose between ES3 and ES5 output modes. We’ve added a new ‘ES6’ option to the targets commandline option:
> tsc --target ES6 myfile.js
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.
We’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. You may need to install an additional .d.ts file for this to work.
Let/Const
The ES6 ‘let’ feature is similar to ‘var’, but it aims to simplify the mental model for the variable’s scope. With ‘let’, you can scope variables to blocks of code rather than whole functions. For example:
functionf() {let total =0;let x =5;for (let x =1; x <10; x++) { total += x; }console.log(x); } f(); // outputs 5
Notice how the two ‘let x’ 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 ‘x’ vars effectively combine into one, leading to rather confusing output.
functionf() {var total =0;var x =5;for (var x =1; x <10; x++) { total += x; }console.log(x); } f(); // outputs 10
TypeScript now supports using ‘let’ and ‘const’ in addition to ‘var’. These currently require the ES6 output mode, but we’re are investigating relaxing this restriction in future versions.
Template Strings
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:
var rectangle = { height: 20, width: 10 };var areaMessage =`Rectangle area is ${rectangle.height* rectangle.width}`;
With the 1.4 release, TypeScript now supports ES6 template strings and can also compile them down to ES3/ES5 expressions.
Looking Ahead
We’re excited to bring new features into TypeScript, including more ECMAScript 6 features and our work on async/await. As always, we’d love to hear your feedback. You can get involved by trying out the release, sending us a bug, playing with the source, and sending us a pull request.