Typescript 3.2 新特性
Max Zhang Lv4

1. bigint 类型

Typescript 3.2 引入了 bigint 类型,它可以表示任意精度的整数。

1
2
let x: bigint = 100n;
let y: bigint = BigInt(100);

不能将 bigint 类型的值赋值给 number 类型的变量,也不能将 number 类型的值赋值给 bigint 类型的变量。

1
2
3
4
5
let x: bigint = 100n;
let y: number = 100;

x = y; // Error
y = x; // Error

2. ts config 继承

Typescript 3.2 引入了 extends 选项,可以继承其他的 tsconfig.json 文件。
通过 compilerOptions 选项可以覆盖继承的 tsconfig.json 文件中的配置。

1
2
3
4
5
6
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
}
}

3. const 断言

Typescript 3.2 引入了 const 断言,可以将一个表达式断言为 const 类型。
不同于 as constconst 断言不会将对象的属性变为 readonly

1
2
let x = { a: 1 } as const;
x.a = 2; // Error

4. Omit 类型

Typescript 3.5 引入了 Omit 类型,它可以从一个类型中排除指定的属性。

1
2
3
4
5
6
7
8
9
10
11
12
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

interface IPerson {
name: string;
age: number;
}

type IStudent = Omit<IPerson, "age">;

let student: IStudent = {
name: "Tom",
};
 评论
评论插件加载失败
正在加载评论插件