Promise.all
的定義,在 3.7
版本中一些混用 null
或 undefined
的時候的問題已經在 3.9
獲得了修復。// @ts-expect-error
新註釋的添加JavaScript
中 CommonJS
的自動引入tsconfig.json
文件TypeScript
定義和書寫規範上的改動和修復之前的 bugs
promise.all
& promise.race
等方法作出了更新,可是也製造出了一個問題。在使用 null
& undefined
尤爲明顯。nterface Lion {
roar(): void
}
interface Seal {
singKissFromARose(): void
}
async function visitZoo(lionExhibit: Promise<Lion>, sealExhibit: Promise<Seal | undefined>) {
let [lion, seal] = await Promise.all([lionExhibit, sealExhibit]);
lion.roar(); // uh oh
// ~~~~
// Object is possibly 'undefined'.
}
複製代碼
這種行爲就很奇怪了,實際上
sealExhibit
當中包含的undefined
,至關因而把undefined
錯誤引入了lion type
當中, 這裏是一個錯誤引用。html
固然在最新的 3.9
版本中修復了這個問題。git
awaited type
awaited type
主要是對如今的 promise
更好的定義和使用。預計在 **`3.9`** 發佈的,結果微軟又跳票了,能夠等下一個版本了。 github
Material-ui
與 Styled-Components
等組件會帶來極差的編輯 / 編譯速度後。主要從聯合類型、交叉類型、條件 判斷的 type 類型以及各類映射 type 類型的性能問題來優化。 把相關的庫編譯時間減小了 40%
左右。Visual Studio Code
團隊提供的建議,咱們發如今執行文件重命名時,單是查明哪些導入語句須要更新就要耗去 5
到 10
秒時間。TypeScript 3.9
調整了內部編譯器與語言服務緩存文件的查找方式,順利解決了這個問題。pull request
的具體優化內容
function hasImportantPermissions(): boolean {
// ...
}
// Oops!
if (hasImportantPermissions) {
// ~~~~~~~~~~~~~~~~~~~~~~~
// This condition will always return true since the function is always defined.
// Did you mean to call it instead?
deleteAllTheImportantFiles();
}
複製代碼
可是,此錯誤僅適用於if語句中的條件。如今三元條件(即語法)如今也支持此功能。好比 cond ? trueExpr : falseExpr
typescript
declare function listFilesOfDirectory(dirPath: string): string[];
declare function isDirectory(): boolean;
function getAllFiles(startFileName: string) {
const result: string[] = [];
traverse(startFileName);
return result;
function traverse(currentPath: string) {
return isDirectory ?
// ~~~~~~~~~~~
// This condition will always return true
// since the function is always defined.
// Did you mean to call it instead?
listFilesOfDirectory(currentPath).forEach(traverse) :
result.push(currentPath);
}
}
複製代碼
import * as fs from "fs";
複製代碼
const fs = require("fs");
複製代碼
TypeScript 如今可以自動檢測您所使用的導入類型,保證文件樣式簡潔而統一。如今有了以下自動引入的功能json
const { readFile } = require('fs')
複製代碼
// before
let f1 = () => 42
// oops - not the same!
let f2 = () => { 42 }
複製代碼
null
斷言操做符(!)
的可選鏈(?.)
行爲不符合直覺。具體來說,在以往的版本中,代碼:promise
foo?.bar!.baz
複製代碼
被解釋爲等效於如下 JavaScript 代碼:緩存
(foo?.bar).baz
複製代碼
在以上代碼中,括號會阻止可選鏈的「短路」行爲;所以若是未定義
foo
爲undefined
,則訪問baz
會引起運行時錯誤。性能優化
換句話說,大多數人認爲以上原始代碼片斷應該被解釋爲在:bash
foo?.bar.baz
複製代碼
中,當 foo
爲 undefined
時,計算結果爲 undefined
。async
這是一項重大變化,但咱們認爲大部分代碼在編寫時都是爲了考慮新的解釋場景。若是您但願繼續使用舊有行爲,則可在!操做符左側添加括號,以下所示:
(foo?.bar)!.baz