TypeScript 1.7
async
/await
support in ES6 targets (Node v4+)
TypeScript now supports asynchronous functions for engines that have native support for ES6 generators, e.g. Node v4 and above.Asynchronous functions are prefixed with theasync
keyword;await
suspends the execution until an asynchronous function return promise is fulfilled and unwraps the value from thePromise
returned.
Example
In the following example, each input element will be printed out one at a time with a 400ms delay:
ts
"use strict";// printDelayed is a 'Promise<void>'asyncfunctionprintDelayed(elements:string[]) {for (constelementofelements) {awaitdelay(400);console.log(element);}}asyncfunctiondelay(milliseconds:number) {returnnewPromise<void>((resolve)=> {setTimeout(resolve,milliseconds);});}printDelayed(["Hello","beautiful","asynchronous","world"]).then(()=> {console.log();console.log("Printed every element!");});
For more information seeasync function reference reference.
Support for--target ES6
with--module
TypeScript 1.7 addsES6
to the list of options available for themodule
option and allows you to specify the module output when targetingES6
.This provides more flexibility to target exactly the features you want in specific runtimes.
Example
{" ": {" ":"amd"," ":"es6"}}
this
-typing
It is a common pattern to return the current object (i.e.this
) from a method to createfluent-style APIs.For instance, consider the followingBasicCalculator
module:
ts
exportdefaultclassBasicCalculator {publicconstructor(protectedvalue:number =0) {}publiccurrentValue():number {returnthis.value;}publicadd(operand:number) {this.value +=operand;returnthis;}publicsubtract(operand:number) {this.value -=operand;returnthis;}publicmultiply(operand:number) {this.value *=operand;returnthis;}publicdivide(operand:number) {this.value /=operand;returnthis;}}
A user could express2 * 5 + 1
as
ts
importcalcfrom"./BasicCalculator";letv =newcalc(2).multiply(5).add(1).currentValue();
This often opens up very elegant ways of writing code; however, there was a problem for classes that wanted to extend fromBasicCalculator
.Imagine a user wanted to start writing aScientificCalculator
:
ts
importBasicCalculatorfrom"./BasicCalculator";exportdefaultclassScientificCalculatorextendsBasicCalculator {publicconstructor(value =0) {super(value);}publicsquare() {this.value =this.value **2;returnthis;}publicsin() {this.value =Math.sin(this.value);returnthis;}}
Because TypeScript used to infer the typeBasicCalculator
for each method inBasicCalculator
that returnedthis
, the type system would forget that it hadScientificCalculator
whenever using aBasicCalculator
method.
For instance:
ts
importcalcfrom"./ScientificCalculator";letv =newcalc(0.5).square().divide(2).sin()// Error: 'BasicCalculator' has no 'sin' method..currentValue();
This is no longer the case - TypeScript now infersthis
to have a special type calledthis
whenever inside an instance method of a class.Thethis
type is written as so, and basically means “the type of the left side of the dot in a method call”.
Thethis
type is also useful with intersection types in describing libraries (e.g. Ember.js) that use mixin-style patterns to describe inheritance:
ts
interfaceMyType {extend<T>(other:T):this &T;}
ES7 exponentiation operator
TypeScript 1.7 supports upcomingES7/ES2016 exponentiation operators:**
and**=
.The operators will be transformed in the output to ES3/ES5 usingMath.pow
.
Example
ts
varx =2 **3;vary =10;y **=2;varz = -(4 **3);
Will generate the following JavaScript output:
js
varx =Math.pow(2,3);vary =10;y =Math.pow(y,2);varz = -Math.pow(4,3);
Improved checking for destructuring object literal
TypeScript 1.7 makes checking of destructuring patterns with an object literal or array literal initializers less rigid and more intuitive.
When an object literal is contextually typed by the implied type of an object binding pattern:
- Properties with default values in the object binding pattern become optional in the object literal.
- Properties in the object binding pattern that have no match in the object literal are required to have a default value in the object binding pattern and are automatically added to the object literal type.
- Properties in the object literal that have no match in the object binding pattern are an error.
When an array literal is contextually typed by the implied type of an array binding pattern:
- Elements in the array binding pattern that have no match in the array literal are required to have a default value in the array binding pattern and are automatically added to the array literal type.
Example
ts
// Type of f1 is (arg?: { x?: number, y?: number }) => voidfunctionf1({x =0,y =0 } = {}) {}// And can be called as:f1();f1({});f1({x:1 });f1({y:1 });f1({x:1,y:1 });// Type of f2 is (arg?: (x: number, y?: number) => voidfunctionf2({x,y =0 } = {x:0 }) {}f2();f2({});// Error, x not optionalf2({x:1 });f2({y:1 });// Error, x not optionalf2({x:1,y:1 });
Support for decorators when targeting ES3
Decorators are now allowed when targeting ES3.TypeScript 1.7 removes the ES5-specific use ofreduceRight
from the__decorate
helper.The changes also inline callsObject.getOwnPropertyDescriptor
andObject.defineProperty
in a backwards-compatible fashion that allows for a to clean up the emit for ES5 and later by removing various repetitive calls to the aforementionedObject
methods.
The TypeScript docs are an open source project. Help us improve these pagesby sending a Pull Request ❤
Last updated: Jul 14, 2025