Announcing TypeScript 1.7

Today, we are thrilled to announce the release of TypeScript 1.7 along with the availability of Visual Studio 2015 Update 1. This release enables async/await by default for ECMAScript 6 (ES6) targets. It also adds support for polymorphic ‘this’ typing, proposed ECMAScript 2016 exponentiation syntax, and ES6 module targeting. For a complete change list, check out our roadmap on GitHub.

As always, you can get your hands on TypeScript 1.7 for Visual Studio 2015 Update 1, Visual Studio 2013, on npm, or straight from the source.

Async/Await for ES6 targets

With the 1.7 release, TypeScript now supports Async functions for targets that have ES6 generator support enabled (e.g. node.js v4 and above). Functions can now be prefixed with the async keyword designating it as an asynchronous function. The await keyword can then be used to stop execution until an async function’s promise is fulfilled. Following is a simple example:

“use strict”;
// printDelayed is a ‘Promise<void>’
async function printDelayed(elements: string[]) {
    for (const element of elements) {
        await delay(200);
        console.log(element);
    }
}
 
async function delay(milliseconds: number) {
    return new Promise<void>(resolve => {
        setTimeout(resolve, milliseconds);
    });
}
 
printDelayed([“Hello”, “beautiful”, “asynchronous”, “world”]).then(() => {
    console.log();
    console.log(“Printed every element!”);
});

We are working on bringing async/await support in TypeScript for other targets, including a breadth of browsers, which might not have ES6 generators support. For more information on current implementation of async/await and how to use it, see our previous blog post.

Polymorphic this Typing

After much community discussion and feedback, TypeScript 1.7 adds a new polymorphic this type. A this type can be used in classes and interfaces to represent some type that is a subtype of the containing type (rather than the containing type itself). This feature makes patterns such as hierarchical fluent APIs much easier to express.

interface Model {
    setupBase(): this;
}
 
interface AdvancedModel extends Model {
    setupAdvanced(): this;
}
 
declare function createModel(): AdvancedModel;
newModel = newModel.setupBase().setupAdvanced(); // fluent style works

For a deep dive on this typing, checkout the TypeScript Wiki.

As a part of supporting the feature, TypeScript 1.7 has made changes in inferring the type from this. In a class, the type of the value this will be inferred to the this type, and subsequent assignments from values of the original type can fail. As a workaround, you could add a type annotation for this. A code sample with recommended work around, along with a list of other potentially breaking changes is available at GitHub.

ES6 Module Emitting

TypeScript 1.7 adds es6 to the list of options available for the –module flag and allows you to specify the module output when targeting ES6. This provides more flexibility to target exactly the features you want in specific runtimes. For example, it is now a breeze to target Node.js v4 and beyond, which doesn’t support ES6 modules (but does support several other ES6 features).

//tsconfig.json targeting node.js v4 and beyond
{
    “compilerOptions”: {
        “module”: “commonjs”,
        “target”: “es6”
    }
}

ES7 Exponentiation

Finally, a little syntactic sugar! The ECMAScript committee recently moved the Exponentiation Operator proposal to stage 3. So we decided it was ready for TypeScript to adopt, and added support for it in TypeScript 1.7.

let squared = 2 ** 2;  // same as: 2 * 2
let cubed = 2 ** 3;  // same as: 2 * 2 * 2
let num = 2;
num **= 2; // same as: num = num * num;

Say goodbye to Math.pow()!

What’s Next?

We are excited to announce all the new improvements we’ve made in this release, and as always, we would love to hear your feedback. Everything we do is easily viewable on our Github. If you’re interested in weighing in on the future of TypeScript, we encourage you to check out our existing issues, throw us a pull request, or just come hang out with the team on gitter.