24 Star 88 Fork 34

wolfx/TypeScript-Documents-Chinese

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
typescript-3.0.md 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
patrick.zhongsp 提交于 4年前 . refactor: structure

TypeScript 3.0

保留关键字 unknown

unknown 现在是一个保留类型名称,因为它现在是一个内置类型。为了支持新引入的 unknown 类型,取决于你对 unknown 的使用方式,你可能需要完全移除变量申明,或者将其重命名。

未开启 strictNullChecks 时,与 null/undefined 交叉的类型会简化到 null/undefined

关闭 strictNullChecks 时,下例中 A 的类型为 null,而 B 的类型为 undefined

type A = { a: number } & null;      // null
type B = { a: number } & undefined; // undefined

这是因为 TypeScript 3.0 更适合分别简化交叉类型和联合类型中的子类型和超类型。但是,因为当 strictNullChecks 关闭时,nullundefined 都被认为是所有其他类型的子类型,与某种对象类型的交集将始终简化为 nullundefined

建议

如果你在类型交叉的情况下依赖 nullundefined 作为单位元,你应该寻找一种方法来使用 unknown 而不是无论它们在哪里都是 nullundefined

参考

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/wolfx/TypeScript-Handbook-zh.git
git@gitee.com:wolfx/TypeScript-Handbook-zh.git
wolfx
TypeScript-Handbook-zh
TypeScript-Documents-Chinese
dev

搜索帮助