联合类型
联合类型,将不同类型联合起来,用 | 隔开,变量只需要满足其中一个类型就可以
let unionType: number | string = 123;
unionType = "hello";
实战:指定多种类型作为新类型的时候使用
// React,Key 类型
type Key = string | number | symbol;
const key: Key = "hello";
交叉类型
交叉类型,将不同类型合并起来,用 & 隔开,变量必须满足所有
type IdProtocal {
id: number;
}
type NameProtocal {
name: string;
}
type OrderProtocal = IdProtocal & NameProtocal;
const order: OrderProtocal = {
id: 123,
name: "hello",
}
字面量
type Version1 = "1.0.0";
const version1: Version1 = "1.0.0";
TypeScript联合类型与交叉类型解析
1万+

被折叠的 条评论
为什么被折叠?



