TypeScript 1.7

在 ES6 目标中支持 async/await(Node v4+)

对于原生支持 ES6 生成器(generator)的引擎(例如 Node v4 及以上版本),TypeScript 现在支持异步函数。异步函数以 async 关键字作为前缀;await 会暂停执行,直到异步函数返回的 Promise 被兑现(fulfilled),并从返回的 Promise 中解包出该值。

示例

在下面的示例中,每个输入元素将以 400ms 的间隔逐个打印出来。

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

更多信息请参阅 async 函数参考

支持 --target ES6--module 联用

TypeScript 1.7 在 module 选项的可选列表中增加了 ES6,并允许在目标为 ES6 时指定模块输出。这为针对特定运行时精确选择所需的功能提供了更大的灵活性。

示例
{
"": "amd",
"": "es6"
}
}

this 类型

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

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 表示为:

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

这种方式通常能带来非常优雅的代码编写体验;然而,对于想要扩展 BasicCalculator 的类来说,存在一个问题。想象一下,如果用户想要开始编写一个 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

例如:

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 类型写出来就是这样,它基本上意味着“方法调用中点号左侧的类型”。

this 类型在结合交叉类型(intersection types)来描述使用 mixin 风格模式描述继承的库(如 Ember.js)时也非常有用。

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

ES7 指数运算符

TypeScript 1.7 支持即将到来的 ES7/ES2016 指数运算符****=。这些运算符在输出时将被转换为使用 Math.pow 的 ES3/ES5 代码。

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

将生成以下 JavaScript 输出:

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

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

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

当一个对象字面量根据对象绑定模式的隐含类型进行上下文类型推断时:

  • 在对象绑定模式中具有默认值的属性在对象字面量中变为可选。
  • 在对象绑定模式中没有匹配项的属性,必须在对象绑定模式中具有默认值,并会被自动添加到对象字面量类型中。
  • 在对象字面量中没有匹配到对象绑定模式的属性,将会报错。

当一个数组字面量根据数组绑定模式的隐含类型进行上下文类型推断时:

  • 在数组绑定模式中没有匹配项的元素,必须在数组绑定模式中具有默认值,并会被自动添加到数组字面量类型中。
示例
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 时使用装饰器

现在当目标为 ES3 时允许使用装饰器。TypeScript 1.7 移除了 __decorate 辅助函数中对 ES5 特有方法 reduceRight 的使用。这些更改还以向后兼容的方式内联了对 Object.getOwnPropertyDescriptorObject.defineProperty 的调用,通过移除上述 Object 方法的多次重复调用,从而优化了 ES5 及后续版本的编译输出。

TypeScript 文档是一个开源项目。请提交 Pull Request 来帮助我们改进这些页面 ❤

此页面的贡献者
MHMohamed Hegazy (52)
OTOrta Therox (13)
JBJack Bates (1)
EBEli Barzilay (1)
TGThomas Güttler (1)
4+

最后更新:2026 年 3 月 27 日