Example
In the following example, each input element will be printed out one at a time with a 400ms delay:
For more information see Async Functions blog post.
Support for —target ES6 with —module
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.
Example
{
"compilerOptions": {
"module": "amd",
"target": "es6"
}
}
It is a common pattern to return the current object (i.e. this
) from a method to create .For instance, consider the following BasicCalculator
module:
export default class BasicCalculator {
public constructor(protected value: number = 0) { }
public currentValue(): number {
return this.value;
}
public add(operand: number) {
this.value += operand;
return this;
public subtract(operand: number) {
this.value -= operand;
return this;
}
this.value *= operand;
return this;
}
public divide(operand: number) {
this.value /= operand;
return this;
}
}
This often opens up very elegant ways of writing code; however, there was a problem for classes that wanted to extend from BasicCalculator
.Imagine a user wanted to start writing a ScientificCalculator
:
import BasicCalculator from "./BasicCalculator";
export default class ScientificCalculator extends BasicCalculator {
public constructor(value = 0) {
super(value);
}
public square() {
this.value = this.value ** 2;
}
public sin() {
this.value = Math.sin(this.value);
return this;
}
}
Because TypeScript used to infer the type BasicCalculator
for each method in that returned this
, the type system would forget that it had ScientificCalculator
whenever using a BasicCalculator
method.
For instance:
import calc from "./ScientificCalculator";
let v = new calc(0.5)
.square()
.divide(2)
.sin() // Error: 'BasicCalculator' has no 'sin' method.
.currentValue();
This is no longer the case - TypeScript now infers this
to have a special type called this
whenever inside an instance method of a class.The this
type is written as so, and basically means “the type of the left side of the dot in a method call”.
ES7 exponentiation operator
TypeScript 1.7 supports upcoming : and
=
.The operators will be transformed in the output to ES3/ES5 using Math.pow
.
Example
var x = 2 ** 3;
var y = 10;
y **= 2;
var z = -(4 ** 3);
Will generate the following JavaScript output:
var x = Math.pow(2, 3);
var y = 10;
y = Math.pow(y, 2);
var z = -(Math.pow(4, 3));
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.
Example
Support for decorators when targeting ES3
Decorators are now allowed when targeting ES3.TypeScript 1.7 removes the ES5-specific use of reduceRight
from the __decorate
helper.The changes also inline calls Object.getOwnPropertyDescriptor
and Object.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 aforementioned methods.