内容简介:TypeScript 3.3 发布了 RC 版本,3.3 是一个平滑的版本,不包含重大更改。 此版本的亮点包括: 改进了调用 union 类型的行为 当 TypeScript 具有 union 类型 A | B 时,允许访问 A 和 B 的交集属性。 interface ...
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 下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
翻转课堂的可汗学院
萨尔曼·可汗(Salman Khan) / 刘婧 / 浙江人民出版社 / 2014-4-1 / 49.00元
MIT和哈佛毕业的高材生缘何放弃金融分析师工作投身教育事业?YouTube上的“可汗学院频道”至今共吸引了163.3万订阅者,观看次数超过3.55亿次,它为什么如此大受欢迎?创始人萨尔曼·可汗阐述属于未来的教育理念——让地球上的任何人都能随时随地享受世界一流的免费教育! 现行教育模式已有200余年历史,可汗认为,在互联网蓬勃发展、社交网络盛况空前的时代,免费、灵活、适合个体、全球共享的教育才......一起来看看 《翻转课堂的可汗学院》 这本书的介绍吧!