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

或者:

猜你喜欢

转载自www.oschina.net/news/103941/typescript-3-3-rc-released
3.3