您现在的位置是:首页 > 文章详情

TypeScript 5.1 正式发布

日期:2023-06-02点击:617

TypeScript 5.1 已正式发布

重要变化

旧代码

 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; });

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

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

 import * as React from "react"; // Both of these are equivalent: const x = <Foo a:b="hello" />; const y = <Foo a : b="hello" />; interface FooProps { "a:b": string; } function Foo(props: FooProps) { return <div>{props["a:b"]}</div>; }
 // 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 = <a:b prop="hello!" />;

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

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

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

自 RC 和 Beta 发布以来的变化

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

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

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

详情查看发布公告

原文链接:https://www.oschina.net/news/243564/typescript-5-1-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章