BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News TypeScript 1.4 Released, Adds Union Types, More ES6 Features

TypeScript 1.4 Released, Adds Union Types, More ES6 Features

This item in japanese

Microsoft delivered TypeScript 1.4, the latest version of their JavaScript language superset. In addition to supporting new TypeScript features such as union types, they've also turned on an ECMAScript 6 target mode.

A highly anticipated feature is union types. Often in JavaScript code, function parameters are checked at runtime to determine behavior. Union types in TypeScript allow for improved type checking in these scenarios. Here's an example from the announcement post:

function f(x: number | number[]) {
  if (typeof x === "number") {
    return x + 10;
  }
  else {
    // return sum of numbers
  }
}

The use of the typeof type guard allows TypeScript to perform different type inference based on the result of the conditional. Facebook's Flow offers a similar type check.

John Reilly, a contributor to the DefinitelyTyped project, shows the difference in the angular-route.d.ts definition file before and after TypeScript 1.4. He says:

Whilst it was possible to overload functions in TypeScript pre 1.4, it was not possible to overload interface members. This meant the only way to model these sorts of properties was by seeking out a best common type which would fit all scenarios. This invariably meant using the any type. Whilst that worked it didn't lend any consuming code a great deal of type safety.

ES6 features let and const are now available with version 1.4, but they will only work when the code is compiled to the new ES6 target. In a future update, Microsoft hopes to extend that support to the ES5 target. A new ES6 feature that does work with ES5 mode is support for basic template strings, which use the back-tick character to enclose the template:

var width = 640;
var height = 480;

var areaDisplay = `The pixel count is ${width * height}`;

Tagged template string support is available when targeting ES6.

Microsoft has said that their goal for TypeScript 2.0 is to "fully support the ECMAScript 6 standard". Version 1.4 is an important update, but there is still much work ahead. According to the Kangax ES6 Compatibility Table, TypeScript's 8% compatibility estimate lags behind the competition. However, the goal of TypeScript is more than just ES6 compatibility; it serves a different purpose than do pure ES6 transpilers such as 6to5.

Looking forward, the TypeScript roadmap indicates that the next release (version 1.5) will include support for for..of and destructuring.

TypeScript 1.4 is available now in Visual Studio 2015 CTP5, Visual Studio 2013, NPM, and on the TypeScript playground.

Rate this Article

Adoption
Style

BT