翻译:疯狂的技术宅
原文:https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript


本文首发微信公众号:jingchengyideng
欢迎关注,每天都给你推送新鲜的前端技术文章


改进了调用联合类型的行为

在TypeScript的早期版本中,不同的联合类型如果想互相访问其取值,它们参数的取值列表必须完全一致才行。


type Fruit = "apple" | "orange";
type Color = "red" | "orange"; type FruitEater = (fruit: Fruit) => number; // 吃水果并对它们排名
type ColorConsumer = (color: Color) => string; // 处理颜色并对其进行描述 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)
// 类型 'FruitEater | ColorConsumer' 没有兼容的调用签名.ts(2349)
f("orange");

不管怎样,在上面的例子中,FruitEaterColorConsumer 都应该能够接受字符串"orange",并返回 numberstring 类型才对。

在TypeScript 3.3中,下面这段代码将不再会报错。


type Fruit = "apple" | "orange";
type Color = "red" | "orange"; type FruitEater = (fruit: Fruit) => number; // 吃水果并对它们排名
type ColorConsumer = (color: Color) => string; // 处理颜色并对其进行描述 declare let f: FruitEater | ColorConsumer; f("orange"); // 可以正常工作!将返回一个'number | string'. f("apple"); // error - Argument of type '"red"' is not assignable to parameter of type '"orange"'. f("red"); // error - Argument of type '"red"' is not assignable to parameter of type '"orange"'.

在TypeScript 3.3中,这些参数会互相交织在一起然后创建新签名。

在上面的例子中, fruitcolor 的参数列表会被交叉到一起产生新的 Fruit&Color 类型的参数。 Fruit & Color会处理为 ("apple" | "orange") & ("red" | "orange") ,它等同于("apple" & "red") | ("apple" & "orange") | ("orange" & "red") | ("orange" & "orange")。 那些不可能的组合被处理成 never,到最后留下了 "orange" & "orange" 这个组合,结果只能是 "orange"

注意
当联合中最多只有一个类型具有多个重载时,这种新行为才会出现,并且联合中最多只能有一个类型具有通用签名。 这意味着 number[] | string[]这种形式 ,在 map (通用)这样的方法中仍然不可以调用。

另一方面,在 forEach 这样的方法中现在可以调用,但是在 noImplicitAny 下可能存在一些问题。


interface Dog {
kind: "dog"
dogProp: any;
}
interface Cat {
kind: "cat"
catProp: any;
} const catOrDogArray: Dog[] | Cat[] = []; catOrDogArray.forEach(animal => {
// ~~~~~~ error!
// 参数'animal'隐式含有'any'类型。
});

在TypeScript 3.3中,这仍然很严格,添加显式类型注释将解决这个问题。


interface Dog {
kind: "dog"
dogProp: any;
}
interface Cat {
kind: "cat"
catProp: any;
} const catOrDogArray: Dog[] | Cat[] = [];
catOrDogArray.forEach((animal: Dog | Cat) => {
if (animal.kind === "dog") {
animal.dogProp;
// ...
}
else if (animal.kind === "cat") {
animal.catProp;
// ...
}
});

使用 --build --watch 检查复合项目的增量文件

TypeScript 3.0 引入了一个用于构建过程的被称为“复合项目”的新功能。 其目的之一是确保用户可以将大型项目拆分为更小的部分,从而能够快速构建,同时保留项目结构,而不会影响现有的 TypeScript 体验。 正式因为有了复合项目,TypeScript 可以用 --build 模式仅重新编译部分项目和依赖项集。 您可以把它视为对项目间构建的优化。

TypeScript 2.7还引入了 --watch 模式,通过新的增量“构建器”API进行构建。 该模式只重新检查和传送被修改的,可能会影响类型检查的源码文件和依赖。 您可以将其视为对项目内构建的优化。

在3.3版本之前,在使用 --build --watch 构建复合项目时,实际上并没有使用这种监视增量文件的基础结构。 在 --build --watch 模式下,如果一个项目中有了更新,将会强制完全重新构建该项目,而不是检查项目中有哪些文件受到影响。

在TypeScript 3.3中, --build 模式的 --watch 标志也可以利用增量文件机制进行监视了。 这可能意味着在 --build --watch 模式下构建速度能将会更快。 在我们的测试中,此功能使--build --watch 的构建时间比原来缩短了50%到75%。 您可以阅读与文件修改时的原始拉取请求相关的更多内容来查看这些数据,我们相信大多数使用复合项目的用户将会在此处得到更好的体验。


本文首发微信公众号:jingchengyideng
欢迎关注,每天都给你推送新鲜的前端技术文章
关注后回复“体系”,检查自己的前端知识体系是否完整


来源:https://segmentfault.com/a/1190000017997665

最新文章

  1. ffmpeg为视频添加时间戳 - 手动编译ffmpeg
  2. 【代码笔记】iOS-水波效果
  3. JDStatusBarNotification和一些宏定义
  4. “用户、组或角色'XXX'在当前数据库中已存在”问题
  5. VMware中网络设置之Bridged
  6. 20160803 - C:\WINDOWS\system32\config\systemprofile\Desktop 不可用的解决
  7. css新增选择器
  8. 开源Launcher - Wox 出炉了
  9. Reveal查看任意app的高级技巧(转)
  10. Swift学习笔记十二
  11. ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller
  12. STL内存分配
  13. perl Socket接收超时设置
  14. 开涛spring3(7.4) - 对JDBC的支持 之 7.4 Spring提供的其它帮助
  15. centos 5.3 安装(samba 3.4.4)
  16. POJ1988 Cube stacking(非递归)
  17. Linux系统初始化配置项(centos7)
  18. btcpool之JobMaker
  19. vc关于大文件读写
  20. Extjs 在项目中碰到问题

热门文章

  1. TClientDataSet数据源设置
  2. android 开发 - 结束所有activity
  3. java中Object转换成int或String类型方法
  4. 电力项目十四--js添加highslider特效
  5. Groovy中的脚本与类
  6. Spoken English Practice(1、This is between you and me, Don't let it out. 2、Don't let your dreams be dreams, no matter how hard it gets, say to yourself, I'm going to make it.)
  7. LMAX Disruptor 原理
  8. 一个误区(关于javascript的字符串拼接)
  9. html禁止选中文字
  10. 调试maven源代码