TypeScript 4.1 正式 GA

2020年11月23日 205点热度 0人点赞 0条评论

图片

喜欢就关注我们吧!

TypeScript 4.1 已正式发布。

使用以下命令通过 npm 获取:

npm install -D typescript

新版本带来了不少新功能:新的检查标志、提升编辑器效率和速度。

  • 引入字符串模板类型

function setVerticalAlignment(pos: "top" | "middle" | "bottom") {// ...}setVerticalAlignment("middel");//                   ~~~~~~~~// error: Argument of type '"middel"' is not assignable to//        parameter of type '"top" | "middle" | "bottom"'.
  • 在映射类型中加入键值重映射 (Key Remapping)

type Options = {
[K in "noImplicitAny" | "strictNullChecks" | "strictFunctionTypes"]?: boolean
};// same as//   type Options = {//       noImplicitAny?: boolean,//       strictNullChecks?: boolean,//       strictFunctionTypes?: boolean//   };
  • 允许递归条件类型

type ElementType<T> =
T extends ReadonlyArray<infer U> ? ElementType<U> : T;function deepFlatten<T extends readonly unknown[]>(x: T): ElementType<T>[] {
throw "not implemented";
}
// All of these return the type 'number[]':
deepFlatten([1, 2, 3]);
deepFlatten([[1], [2, 3]]);
deepFlatten([[1], [[2]], [[[3]]]]);
  • 新增检查索引访问功能 noUncheckedIndexedAccess

interface Options {
path: string;
permissions: number;// Extra properties are caught by this index signature.[propName: string]: string | number;
}function checkOptions(opts: Options) {
opts.path // stringopts.permissions // number// These are all allowed too!// They have the type 'string | number'.opts.yadda.toString();
opts["foo bar baz"].toString();
opts[Math.random()].toString();
}
  • 使用 paths 启用路径映射时可以不指定 baseUrl

  • checkJs 现在默认意味着 allowJs ,不再需要同时设置 checkJs  allowJs

  • 支持 React 17 的 jsx 和 jsxdev 功能

  • 编辑器支持 JSDoc  @see Tag

// @filename: first.tsexport class C { }// @filename: main.tsimport * as first from './first';/**
* @see first.C
*/
function related() { }

微信聊天窗口回复”1123“查看发布公告。


▼ 往期精彩回顾 ▼

Chrome 87 发布,获多年来最大性能提升

13岁小男孩构建了一个RISC-V内核,厉害了!

VUE 新语法糖魔改 JavaScript 引争议

约五十万个“歧视性”单词被计划替换,影响数千项目

阿里将入局树莓派的微型计算机市场

图片

图片觉得不错,请点个在看

45070TypeScript 4.1 正式 GA

这个人很懒,什么都没留下

文章评论