async/await 編譯到 ES6 (Node v4+)TypeScript 目前在已經(jīng)原生支持 ES6 generator 的引擎 (比如 Node v4 及以上版本) 上支持異步函數(shù). 異步函數(shù)前置 async 關(guān)鍵字; await 會(huì)暫停執(zhí)行, 直到一個(gè)異步函數(shù)執(zhí)行后返回的 promise 被 fulfill 后獲得它的值.
在下面的例子中, 輸入的內(nèi)容將會(huì)延時(shí) 200 毫秒逐個(gè)打印:
"use strict";
// printDelayed 返回值是一個(gè) 'Promise<void>'
async function printDelayed(elements: string[]) {
for (const element of elements) {
await delay(200);
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("打印每一個(gè)內(nèi)容!");
});
查看 Async Functions 一文了解更多.
--target ES6 和 --moduleTypeScript 1.7 將 ES6 添加到了 --module 選項(xiàng)支持的選項(xiàng)的列表, 當(dāng)編譯到 ES6 時(shí)允許指定模塊類型. 這讓使用具體運(yùn)行時(shí)中你需要的特性更加靈活.
{
"compilerOptions": {
"module": "amd",
"target": "es6"
}
}
this 類型在方法中返回當(dāng)前對(duì)象 (也就是 this) 是一種創(chuàng)建鏈?zhǔn)?API 的常見(jiàn)方式. 比如, 考慮下面的 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();
這使得這么一種優(yōu)雅的編碼方式成為可能; 然而, 對(duì)于想要去繼承 BasicCalculator 的類來(lái)說(shuō)有一個(gè)問(wèn)題. 想象使用者可能需要編寫一個(gè) 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;
}
}
因?yàn)?BasicCalculator 的方法返回了 this, TypeScript 過(guò)去推斷的類型是 BasicCalculator, 如果在 ScientificCalculator 的實(shí)例上調(diào)用屬于 BasicCalculator 的方法, 類型系統(tǒng)不能很好地處理.
舉例來(lái)說(shuō):
import calc from "./ScientificCalculator";
let v = new calc(0.5)
.square()
.divide(2)
.sin() // Error: 'BasicCalculator' 沒(méi)有 'sin' 方法.
.currentValue();
這已經(jīng)不再是問(wèn)題 - TypeScript 現(xiàn)在在類的實(shí)例方法中, 會(huì)將 this 推斷為一個(gè)特殊的叫做 this 的類型. this 類型也就寫作 this, 可以大致理解為 "方法調(diào)用時(shí)點(diǎn)左邊的類型".
this 類型在描述一些使用了 mixin 風(fēng)格繼承的庫(kù) (比如 Ember.js) 的交叉類型:
interface MyType {
extend<T>(other: T): this & T;
}
TypeScript 1.7 支持將在 ES7/ES2016 中增加的冪運(yùn)算符: ** 和 **=. 這些運(yùn)算符會(huì)被轉(zhuǎn)換為 ES3/ES5 中的 Math.pow.
var x = 2 ** 3;
var y = 10;
y **= 2;
var z = -(4 ** 3);
會(huì)生成下面的 JavaScript:
var x = Math.pow(2, 3);
var y = 10;
y = Math.pow(y, 2);
var z = -(Math.pow(4, 3));
TypeScript 1.7 使對(duì)象和數(shù)組字面量解構(gòu)初始值的檢查更加直觀和自然.
當(dāng)一個(gè)對(duì)象字面量通過(guò)與之對(duì)應(yīng)的對(duì)象解構(gòu)綁定推斷類型時(shí):
當(dāng)一個(gè)數(shù)組字面量通過(guò)與之對(duì)應(yīng)的數(shù)組解構(gòu)綁定推斷類型時(shí):
// f1 的類型為 (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 });
// f2 的類型為 (arg?: (x: number, y?: number) => void
function f2({ x, y = 0 } = { x: 0 }) { }
f2();
f2({}); // 錯(cuò)誤, x 非可選
f2({ x: 1 });
f2({ y: 1 }); // 錯(cuò)誤, x 非可選
f2({ x: 1, y: 1 });
裝飾器現(xiàn)在可以編譯到 ES3. TypeScript 1.7 在 __decorate 函數(shù)中移除了 ES5 中增加的 reduceRight. 相關(guān)改動(dòng)也內(nèi)聯(lián)了對(duì) Object.getOwnPropertyDescriptor 和 Object.defineProperty 的調(diào)用, 并向后兼容, 使 ES5 的輸出可以消除前面提到的 Object 方法的重復(fù)[1].