IT资讯 TypeScript 5.1 正式发布

caspar · 2023-06-02 21:30:08 · 热度: 6

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 发布以来,开发团队对内置重构进行了轻微迭代,以将声明移至现有文件。

详情查看发布公告

为您推荐与 typescript 相关的帖子:

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册