TypeScript 1.7
在 ES6 目标中支持 async/await(Node v4+)
TypeScript 现在为原生支持 ES6 生成器的引擎(例如 Node v4 及以上)提供了异步函数支持。 异步函数以 async 关键字为前缀; await 会暂停执行,直到异步函数返回的 promise 被完成,并从返回的 Promise 中解包值。
示例
在以下示例中,每个输入元素将依次打印,延迟 400 毫秒:
"use strict";
// printDelayed 是一个 '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!");
});更多信息请参阅异步函数参考。
支持 --target ES6 与 --module
TypeScript 1.7 将 ES6 添加到 module 选项的可用值列表中,并允许你在以 ES6 为目标时指定模块输出。 这为在特定运行时中精确指定所需功能提供了更大的灵活性。
示例
{
"compilerOptions": {
"module": "amd",
"target": "es6"
}
}this 类型
从方法中返回当前对象(即 this)以创建流畅风格 API 是一种常见模式。 例如,考虑以下 BasicCalculator 模块:
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 表示为:
import calc from "./BasicCalculator";
let v = new calc(2).multiply(5).add(1).currentValue();这通常开启了非常优雅的代码编写方式;然而,对于想要从 BasicCalculator 扩展的类来说存在一个问题。 假设用户想要开始编写一个 ScientificCalculator:
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 类型。
例如:
import calc from "./ScientificCalculator";
let v = new calc(0.5)
.square()
.divide(2)
.sin() // 错误:'BasicCalculator' 没有 'sin' 方法。
.currentValue();这种情况不再发生——现在 TypeScript 在类实例方法内部将 this 推断为一种称为 this 的特殊类型。 this 类型的写法如此,基本上意味着“方法调用中点左侧的类型”。
this 类型在描述使用 mixin 风格模式继承的库(例如 Ember.js)时,与交集类型结合也很有用:
interface MyType {
extend<T>(other: T): this & T;
}ES7 幂运算符
TypeScript 1.7 支持即将到来的 ES7/ES2016 幂运算符:** 和 **=。 这些运算符将在输出中转换为使用 Math.pow 的 ES3/ES5 代码。
示例
var x = 2 ** 3;
var y = 10;
y **= 2;
var z = -(4 ** 3);将生成以下 JavaScript 输出:
var x = Math.pow(2, 3);
var y = 10;
y = Math.pow(y, 2);
var z = -Math.pow(4, 3);改进的解构对象字面量检查
TypeScript 1.7 使带有对象字面量或数组字面量初始化器的解构模式检查变得不那么严格,更直观。
当对象字面量被对象绑定模式的隐含类型上下文类型化时:
- 对象绑定模式中带有默认值的属性在对象字面量中变为可选。
- 对象绑定模式中在对象字面量中没有匹配的属性,在对象绑定模式中必须有默认值,并自动添加到对象字面量类型中。
- 对象字面量中在对象绑定模式中没有匹配的属性是错误。
当数组字面量被数组绑定模式的隐含类型上下文类型化时:
- 数组绑定模式中在数组字面量中没有匹配的元素,在数组绑定模式中必须有默认值,并自动添加到数组字面量类型中。
示例
// f1 的类型为 (arg?: { x?: number, y?: number }) => void
function f1({ x = 0, y = 0 } = {}) {}
// 可以这样调用:
f1();
f1({});
f1({ x: 1 });
f1({ y: 1 });
f1({ x: 1, y: 1 });
// f2 的类型为 (arg?: (x: number, y?: number) => void
function f2({ x, y = 0 } = { x: 0 }) {}
f2();
f2({}); // 错误,x 不是可选的
f2({ x: 1 });
f2({ y: 1 }); // 错误,x 不是可选的
f2({ x: 1, y: 1 });在以 ES3 为目标时支持装饰器
现在在以 ES3 为目标时允许使用装饰器。 TypeScript 1.7 从 __decorate 辅助函数中移除了 ES5 特定的 reduceRight 使用。 这些更改还以向后兼容的方式内联了 Object.getOwnPropertyDescriptor 和 Object.defineProperty 的调用,从而允许通过移除对上述 Object 方法的各种重复调用来清理 ES5 及更高版本的输出。