TypeScript 3.3 RC 釋出,增量構建,時間縮短超一半
TypeScript 3.3 釋出了 RC 版本,3.3 是一個平滑的版本,不包含重大更改。
此版本的亮點包括:
改進了呼叫 union 型別的行為
當 TypeScript 具有 union 型別 A | B 時,允許訪問 A 和 B 的交集屬性。
interface A { aProp: string; commonProp: string; } interface B { bProp: number; commonProp: number } type Union = A | B; declare let x: Union; x.aProp; // error - 'B' doesn't have the property 'aProp' x.bProp; // error - 'A' doesn't have the property 'bProp' x.commonProp; // okay! Both 'A' and 'B' have a property named `commonProp`.
很顯然,只有當 A 和 B 中都有某一個屬性時,它才可以被呼叫。
但是把這個場景擴充套件到處理屬性型別的時候,事情就不一樣了:
type CallableA = (x: boolean) => string; type CallableB = (x: boolean) => number; type CallableUnion = CallableA | CallableB; declare let f: CallableUnion; let x = f(true); // Okay! Returns a 'string | number'.
union 中 A 和 B 共有的屬性它們的型別不同,再極端一點:
type Fruit = "apple" | "orange"; type Color = "red" | "orange"; type FruitEater = (fruit: Fruit) => number;// eats and ranks the fruit type ColorConsumer = (color: Color) => string;// consumes and describes the colors declare let f: FruitEater | ColorConsumer; // Cannot invoke an expression whose type lacks a call signature. //Type 'FruitEater | ColorConsumer' has no compatible call signatures.ts(2349) f("orange");
TypeScript 3.3 中,這不再會產生錯誤:
type Fruit = "apple" | "orange"; type Color = "red" | "orange"; type FruitEater = (fruit: Fruit) => number;// eats and ranks the fruit type ColorConsumer = (color: Color) => string;// consumes and describes the colors declare let f: FruitEater | ColorConsumer; f("orange"); // It works! Returns a 'number | string'. f("apple");// error - Argument of type '"apple"' is not assignable to parameter of type '"orange"'. f("red");// error - Argument of type '"red"' is not assignable to parameter of type '"orange"'.
--build 模式下 --watch 複合專案的增量檔案
TypeScript 3.0 引入了一個用於構建稱為“複合專案”的構建新功能,它可以確保使用者將大型專案拆分為更小的部分,從而快速構建並保留專案結構,而不會影響現有的 TypeScript 體驗。TypeScript 可以使用 --build 模式僅重新編譯專案和依賴項集。
去年團隊通過新的增量“builder” API 釋出了優化的 --watch 模式,該模式僅重新檢查已更改的檔案或其依賴性可能影響型別檢查的檔案。
這兩種模式,一個作用於專案間,一個作用於專案內。但是在使用 --build --watch 進行復合專案構建時,一個專案中的更新將強制完整構建整個專案,而不是確定該專案中的哪些檔案受到影響。
TypeScript 3.3 中改進了這一點,現在 --build 模式也可以利用 --watch 只確定增量檔案是否受影響的功能。這意味著在 --build --watch 模式下可以更快地進行構建。在測試中,此功能使得原始 --build --watch 構建時間縮短了 50% 到 75%。
詳情檢視釋出公告 。
通過 NuGet 獲取新版本:
npm install -g typescript@rc
或者:
-
Downloading for Visual Studio 2017 (for version 15.2 or later)
-
Sublime Text 下載