📜 ⬆️ ⬇️

Release typescript 1.7

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

async / await, but not for all


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

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


Polymorphic this


Now for the return value of the function, you can specify this . 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 from this version, it is possible to more flexibly configure in which javascript to compile and for which system of work with modules. For example, you can collect ES6 code, but with commonjs modules, for nodes:

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


ES7 Exponentiation


A small syntax sugar from ES7, which came to replace 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?


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

 npm update typescript 

Source: https://habr.com/ru/post/272037/


All Articles