TypeScript 1.7

ES6 目标平台(Node v4+)支持 async/await

¥async/await support in ES6 targets (Node v4+)

TypeScript 现在支持原生支持 ES6 生成器的引擎的异步函数,例如 Node v4 及更高版本。异步函数以 async 关键字为前缀;await 会暂停执行,直到异步函数返回 promise 得到满足,并从返回的 Promise 中解包值。

¥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 the async keyword; await suspends the execution until an asynchronous function return promise is fulfilled and unwraps the value from the Promise returned.

示例

¥Example

在以下示例中,每个输入元素将以 400 毫秒的延迟逐个打印:

¥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>'
async function printDelayed(elements: string[]) {
for (const element of elements) {
await delay(400);
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!");
});

更多信息请参阅 异步函数引用 参考。

¥For more information see async function reference reference.

支持 --target ES6--module

¥Support for --target ES6 with --module

TypeScript 1.7 将 ES6 添加到 module 选项的可用选项列表中,并允许你在面向 ES6 时指定模块输出。这提供了更大的灵活性,可以精确定位你在特定运行时所需的功能。

¥TypeScript 1.7 adds ES6 to the list of options available for the module option 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

{
"": "amd",
"": "es6"
}
}

this 类型

¥this-typing

从方法中返回当前对象(即 this)来创建 流畅风格的 API 是一种常见的模式。例如,考虑以下 BasicCalculator 模块:

¥It is a common pattern to return the current object (i.e. this) from a method to create fluent-style APIs. For instance, consider the following BasicCalculator module:

ts
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;
}
public multiply(operand: number) {
this.value *= operand;
return this;
}
public divide(operand: number) {
this.value /= operand;
return this;
}
}

用户可以将 2 * 5 + 1 表示为

¥A user could express 2 * 5 + 1 as

ts
import calc from "./BasicCalculator";
let v = new calc(2).multiply(5).add(1).currentValue();

这通常会开启非常优雅的代码编写方式;但是,对于想要从 BasicCalculator 扩展的类,存在一个问题。想象一下,一位用户想要开始编写 ScientificCalculator

¥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:

ts
import BasicCalculator from "./BasicCalculator";
export default class ScientificCalculator extends BasicCalculator {
public constructor(value = 0) {
super(value);
}
public square() {
this.value = this.value ** 2;
return this;
}
public sin() {
this.value = Math.sin(this.value);
return this;
}
}

由于 TypeScript 过去会为 BasicCalculator 中每个返回 this 的方法推断出其类型 BasicCalculator,因此每当使用 BasicCalculator 方法时,类型系统就会忘记它具有 ScientificCalculator

¥Because TypeScript used to infer the type BasicCalculator for each method in BasicCalculator that returned this, the type system would forget that it had ScientificCalculator whenever using a BasicCalculator method.

例如:

¥For instance:

ts
import calc from "./ScientificCalculator";
let v = new calc(0.5)
.square()
.divide(2)
.sin() // Error: 'BasicCalculator' has no 'sin' method.
.currentValue();

现在情况已不再如此 - 现在,只要在类的实例方法中,TypeScript 就会推断 this 具有一个名为 this 的特殊类型。this 类型写成 so,其基本含义是 “方法调用中点左侧的类型”。

¥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”.

this 类型在描述使用 mixin 风格模式描述继承的库(例如 Ember.js)时,也适用于交叉类型:

¥The this type is also useful with intersection types in describing libraries (e.g. Ember.js) that use mixin-style patterns to describe inheritance:

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

ES7 幂运算运算符

¥ES7 exponentiation operator

TypeScript 1.7 支持即将推出的 ES7/ES2016 幂运算符****=。操作符将使用 Math.pow 在输出中转换为 ES3/ES5。

¥TypeScript 1.7 supports upcoming ES7/ES2016 exponentiation operators: ** and **=. The operators will be transformed in the output to ES3/ES5 using Math.pow.

示例

¥Example

ts
var x = 2 ** 3;
var y = 10;
y **= 2;
var z = -(4 ** 3);

将生成以下 JavaScript 输出:

¥Will generate the following JavaScript output:

js
var x = Math.pow(2, 3);
var y = 10;
y = Math.pow(y, 2);
var z = -Math.pow(4, 3);

改进了对解构对象字面量的检查

¥Improved checking for destructuring object literal

TypeScript 1.7 使得使用对象字面量或数组字面量初始化器检查解构模式变得不那么死板,更加直观。

¥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 }) => void
function f1({ 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) => void
function f2({ x, y = 0 } = { x: 0 }) {}
f2();
f2({}); // Error, x not optional
f2({ x: 1 });
f2({ y: 1 }); // Error, x not optional
f2({ x: 1, y: 1 });

支持 ES3 目标平台下的装饰器

¥Support for decorators when targeting ES3

现在支持 ES3 的装饰器。TypeScript 1.7 从 __decorate 助手中删除了 ES5 特有的 reduceRight 用法。这些更改还以向后兼容的方式内联调用 Object.getOwnPropertyDescriptorObject.defineProperty,从而允许通过删除对上述 Object 方法的各种重复调用来清理 ES5 及更高版本的输出。

¥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 Object methods.