Skip to main content

类型别名

类型别名

TypeScript 提供了为类型注解设置别名的便捷语法,你可以使用 type 来创建类型别名:

type StrOrNum = string | number;

// 使用
let sample: StrOrNum;
sample = 123;
sample = '123';
sample = true; // Error

类型别名常用于联合类型。

tip

如果你需要使用类型注解的层次结构,请使用 interface

参考文档

深入理解TypeScript