TypeScript 5.1 正式发布

您所在的位置:网站首页 未定义的返回值 TypeScript 5.1 正式发布

TypeScript 5.1 正式发布

2023-06-05 03:50| 来源: 网络整理| 查看: 265

TypeScript 5.1 已正式发布。

重要变化

更智能地检查未定义返回值的函数 (undefined-Returning Functions)

旧代码

function foo() { // no return } // x = undefined let x = foo(); // fine - we inferred that 'f1' returns 'void' function f1() { // no returns } // fine - 'void' doesn't need a return statement function f2(): void { // no returns } // fine - 'any' doesn't need a return statement function f3(): any { // no returns } // error! // A function whose declared type is neither 'void' nor 'any' must return a value. function f4(): undefined { // no returns }

新代码

// Works in TypeScript 5.1! function f4(): undefined { // no returns } // Works in TypeScript 5.1! takesFunction((): undefined => { // no returns }); // Works in TypeScript 5.1! takesFunction(function f() { // ^ return type is undefined // no returns }); // Works in TypeScript 5.1! takesFunction(function f() { // ^ return type is undefined return; }); Getters 和 Setters 类型之间无限制

TypeScript 5.1 移除了 Get 访问器的返回类型必须可分配给其 Set 访问器类型 这一限制。

携带命名空间的 JSX 标签

使用 JSX 时,TypeScript 现在支持命名空间属性名称。

import * as React from "react"; // Both of these are equivalent: const x = ; const y = ; interface FooProps { "a:b": string; } function Foo(props: FooProps) { return {props["a:b"]}; } // In some library's code or in an augmentation of that library: namespace JSX { interface IntrinsicElements { ["a:b"]: { prop: string }; } } // In our code: let x = ; 对 @paramJSDoc Tags 自动补全 Snippet

TypeScript 5.1 支持在 TypeScript 和 JavaScript 文件中输入 @param 标记时的代码片段完成,帮助开发者在编写代码文档或在 JavaScript 中添加 JSDoc 类型时快速生成对应注释信息。

JSX 元素和 JSX Tag 类型之间的解耦类型检查

TypeScript 使用 JSX 的一个痛点是它对每个 JSX 元素标签类型的要求。TypeScript 5.1 让 JSX 库可以更准确地描述 JSX 组件可以返回的内容。

对于许多人来说,这意味着可以在 React 中使用异步服务器组件。

其他变化

支持在 Module Resolution 中查询 typeRoots Linked Cursors for JSX Tags 其他优化 破坏性变更

自 RC 和 Beta 发布以来的变化

自 Beta 发布以来,开发团队已纠正装饰器中inithook的一些行为,社区提议的行为已经过调整。此外还对isolatedModules下的 emit 行为进行了更改,确保脚本文件不会被重写为模块。

这也意味着transpileModuleAPI 的使用也将确保脚本文件不会被解释为模块,因为它假定使用isolatedModules。

自 RC 发布以来,开发团队对内置重构进行了轻微迭代,以将声明移至现有文件。

详情查看发布公告。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3