days
-1
-1
hours
0
-9
minutes
-2
0
seconds
-4
-1
search
Check your Type

TypeScript 1.7 is here with async/await as default for ES6

Natali Vlatko
Typewriter image via Shutterstock

A new version of TypeScript has been unveiled with long awaited support for async functions for ECMAScript 6 (ES6). Future support plans are already in place for ES3 and ES5, too. TypeScript 1.7 also includes polymorphic this typing plus some breaking changes.

The newest instalment of Microsoft’s JavaScript extension TypeScript has landed with async/await enabled by default for ECMAScript 6 (ES6) targets. This means ES6 generators such as Node v4 and above will now be able to call asynchronous methods without blocking for the asynchronous operations to complete.

The following example from GitHub has been provided for reference purposes:

"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!");
});

Looking at TypeScript’s roadmap for future releases, the team are also working on having async/await support for ES3 and ES5 by version 2.0. ES6 has also been added to the list of options available for the --module flag and allows you to specify the module output when targeting it.

Another new feature is the introduction of polymorphic this typing for creating fluent-style APIs. The this type is also said to be effective with intersection types in describing libraries like Ember.js that use mixin-style patterns to describe inheritance:

interface MyType {
    extend<T>(other: T): this & T;
}

Last but not least, the team at Microsoft have clued in to the ECMAScript committee recently moving the Exponentiation Operator proposal to stage 3 and have brought in support for operators ** and **= in version 1.7. The operators will be transformed in the output to ES3/ES5 using Math.pow.

Breaking changes

As for breaking changes, the type of the value this in a class will now be inferred to the this type. It has been recommended that users add a a type annotation such as in the example below:

class Fighter {
    /** @returns the winner of the fight. */
    fight(opponent: Fighter) {
        let theVeryBest: Fighter = this;
        if (Math.rand() < 0.5) {
            theVeryBest = opponent; // no error
        }
        return theVeryBest
    }
}

The keywords abstract, public, protected and private are FutureReservedWords in ES3 and are subject to automatic semicolon insertion (ASI). TypeScript never used to insert semicolons when these keywords were on their own line, but this has now been fixed. In order to avoid issues, remove line breaks after keywords when defining class members and avoid relying on ASI.

For a more detailed rundown of TypeScript’s new shipment, visit their repository on GitHub.

 

Author
Natali Vlatko
An Australian who calls Berlin home, via a two year love affair with Singapore. Natali was an Editorial Assistant for JAXenter.com (S&S Media Group).

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of