TypeScript 4.3 Beta 发布

栏目: 软件资讯 · 发布时间: 4年前

内容简介:TypeScript 4.3 Beta 已发布。此版本更新亮点包括:支持自动补全 import 语句、改进 Template String Type(模板字符串类型)、@link标签的编辑器支持、引入static索引签名功能等。 支持自动补全 import 语句 通过...

TypeScript 4.3 Beta 已发布。此版本更新亮点包括:支持自动补全 import 语句、改进 Template String Type(模板字符串类型)、@link标签的编辑器支持、引入static索引签名功能等。

支持自动补全 import 语句

通过利用 auto-imports 功能(原理是提供所有可能的导出,并在文件顶部自动插入导入语句),当开发者输入 import 关键字后即可生成完整的导入语句,其中包括所要编写的路径。

TypeScript 4.3 Beta 发布

改进 Template String Type

Template String Type(模板字符串类型)是最近版本才引入的类型构造,这些类型可以通过级联来构造新的类似字符串的类型:

type Color = "red" | "blue";
type Quantity = "one" | "two";

type SeussFish = `${Quantity | Color} fish`;
// same as
//   type SeussFish = "one fish" | "two fish"
//                  | "red fish" | "blue fish";

或匹配其他类似字符串类型的模式:

declare let s1: `${number}-${number}-${number}`;
declare let s2: `1-2-3`;

// Works!
s1 = s2;

此版本的改进包括:在 TypeScript 推断模板字符串类型的时候,当模板字符串由类似字符串字面量的类型在上下文中类型化时,它将尝试为该表达式指定模板类型。

function bar(s: string): `hello ${string}` {
    // Previously an error, now works!
    return `hello ${s}`;
}

另一项改进为 TypeScript 现在可以更好地关联不同看的模板字符串类型,并在它们之间进行推断。

declare let s: string;
declare function f<T extends string>(x: T): T;

// Previously: string
// Now       : `hello-${string}`
let x2 = f(`hello ${s}`);

@link标签的编辑器支持

TypeScript 现在支持解析@link标签链接的声明。

/**
 * This function depends on {@link bar}
 */
function foo() {

}

function bar() {

}

引入static索引签名功能

索引签名 (Index Signatures) 支持在某个值上设置比类型显式声明更多的属性。

class Foo {
    hello = "hello";
    world = 1234;

    // This is an index signature:
    [propName: string]: string | number | undefined;
}

let instance = new Foo();

// Valid assigment
instance["whatever"] = 42;

// Has type 'string | number | undefined'.
let x = instance["something"];

详情查看 发布公告


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First Python

Head First Python

Paul Barry / O'Reilly Media / 2010-11-30 / USD 49.99

Are you keen to add Python to your programming skills? Learn quickly and have some fun at the same time with Head First Python. This book takes you beyond typical how-to manuals with engaging images, ......一起来看看 《Head First Python》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试