Typescript 1.7 Release

    Greetings, colleagues! Yesterday Microsoft tensed up and rolled out the release of the next version of typescript, a typed add-on for javascript. In the new version, not only exponentiation and separate configuration of goals, but also the first developments on async / await from the ES7 standard. More details under the cut.

    async / await, but not for everyone


    The main feature that has been around for a long time in the beta version is support for the async and await keywords for working with promises at the language level. This syntax is being developed within the framework of the following ES7 standard: one of the features of Typescript is that it drags language features not only from ES6, but also from ES7. Unfortunately, the current version of typescript can only generate async and await code for js interpreters with generator support. So far from such node.js is the fourth version. The authors promise to make support for the / ES5 browser, but complain that such an implementation requires the state machine to create a state machine, which greatly complicates the whole compilation process.

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


    Polymorphic this


    Now you can specify this for the return value of the function . This means - "an object of this class or any class inherited from it." It is very convenient to create a "fluent api" when the method returns an object of a previously unknown type:

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


    --module now supports ES6


    Starting with this version, you can more flexibly configure which javascript to compile and for which system of working with modules. For example, you can compile ES6 code, but with commonjs modules, for a node:

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


    ES7 exponentiation


    A small syntactic sugar from ES7, which replaced Math.pow :

    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;
    


    Where to get it?


    The latest version of typescript is traditionally available on the official website and in npm:

    npm update typescript
    

    Also popular now: