关于前端:4-个-TypeScript-51-Beta-版重要更新内容

4次阅读

共计 3095 个字符,预计需要花费 8 分钟才能阅读完成。

TypeScript 5.1 在 5.0 公布后不久就公布了测试版,但测试版不代表最终正式版。

官网原文 Announcing TypeScript 5.1 Beta 中提供了残缺的更新内容,以下是我梳理的 TypeScript 5.1 次要更新内容:

  1. 改良了函数返回值类型 undefined 的类型推断
  2. GetterSetter 当初反对设置不同的类型
  3. 主动补全 JSDoc @param 标签的代码片段
  4. TypeScript 5.1 至多须要运行在 ES2020Node.js 14.17 的运行时环境中

想要尝鲜的敌人,能够通过 npm 疾速装置最新测试版:

npm install -D typescript@beta

1. 改良函数返回值类型 undefined 的类型推断

在 JavaScript 中,函数如果没有返回值,会主动返回 undefined

// no return
const test = () => {}
test();  // undefined

在晚期版本的 TypeScript 中,只有返回值类型为 voidany 的函数能够没有 return 语句,即便你分明这个函数返回值是 undefined,你也须要至多有一个 return 语句。

//  ✅ return 'void'
const t1 = () => {}

//  ✅ 'void' doesn't need a return statement
const t2 = (): void => {}

// ✅  'any' doesn't need a return statement
const t3 = (): any => {}

// ❌ A function whose declared type is neither 'void' nor 'any' must return a value.
const t4 = (): undefined => {}

当你心愿函数返回 undefined,你能够有 2 种形式:

  1. 应用 return undefined 语句;
  2. 应用 return 语句并且定义返回值类型为 undefined
declare function fun(f: () => undefined): undefined;

// ❌ Argument of type '() => void' is not assignable to parameter of type '() => undefined'.
fun(() => {})

// ❌ A function whose declared type is neither 'void' nor 'any' must return a value.
fun((): undefined => {})

// ❌ Argument of type '() => void' is not assignable to parameter of type '() => undefined'.
fun(() => {return;})

// ✅ 
fun(() => {return undefined;});

// ✅ 
fun((): undefined => {return;});

为了解决这种困惑,TypeScript 5.1 反对容许函数返回 undefined 时不须要 return 语句。

// ✅ TypeScript 5.1!
const f1 = (): undefined => {}

// ✅ TypeScript 5.1!
f2((): undefined => {})

2. Getter 和 Setter 反对设置不同类型

在 TypeScript 4.3 容许为 GetterSetter 指定不同类型。

interface Serializer {set value(v: string | number | boolean);
  get value(): string;}

declare let box: Serializer;

// ✅ Allows writing a 'boolean'
box.value = true;

// ✅ Comes out as a 'string'
console.log(box.value.toUpperCase());

// ❌ Property 'toFixed' does not exist on type 'string'.
console.log(box.value.toFixed());

最后,咱们要求 get 类型必须是 set 类型的子类型,这种写法很无效。

box.value = box.value;

然而,很多现有 API 的 GetterSetter 之间存在齐全不相干的类型。例如,DOM 和 CSSStyleRule API 中的 style 属性。每个款式规定都有一个 CSSStyleDeclaration 类型的 style 属性,但你只能应用字符串批改它。

TypeScript 5.1 反对为 GetterSetter 设置不同类型:

interface CSSStyleRule {
  // ...

  /** Always reads as a `CSSStyleDeclaration` */
  get style(): CSSStyleDeclaration;

  /** Can only write a `string` here. */
  set style(newValue: string);

  // ...
}

也反对上面这样应用:

class SafeBox {
  #value: string | undefined;

  // Only accepts strings!
  set value(newValue: string) {}

  // Must check for 'undefined'!
  get value(): string | undefined {return this.#value;}
}

实际上,这相似于在 --exactOptionalProperties 下查看可选属性的形式。

3. 主动补全 JSDoc @param 标签的代码片段

TypeScript 5.1 反对在 TypeScript 和 JavaScript 文件中输出 @param 标记时的代码片段实现。帮忙咱们在编写代码文档或在 JavaScript 中增加 JSDoc 类型时疾速生成对应正文信息。

4. 最低运行时要求:ES2020 和 Node.js 14.17

TypeScript 5.1 反对 ECMAScript 2020 新个性,因而须要在较新的 Node.js 运行环境下应用,至多须要 Node.js 14.17 版本以上。旧版 Node.js 可能导致 tsc.jstsserver.js 运行谬误。

node_modules/typescript/lib/tsserver.js:2406
  for (let i = startIndex ?? 0; i < array.length; i++) {
                           ^
SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

总结

TypeScript 5.1 目前还在测试阶段,预计会在接下来的几周内公布候选版本和最终稳固版本。如果你对 TpeScript 感兴趣,能够装置测试版尝试体验一下。

正文完
 0