类型别名,就是给类型起一个别名,让它可以更方便的被重用。
let sum: (x: number, y: number) => number const result = sum(1,2) type PlusType = (x: number, y: number) => number let sum2: PlusType // 支持联合类型 type StrOrNumber = string | number let result2: StrOrNumber = '123' result2 = 123 // 字符串字面量 type Directions = 'Up' | 'Down' | 'Left' | 'Right' let toWhere: Directions = 'Up' 复制代码
interface IName { name: string } type IPerson = IName & { age: number } let person: IPerson = { name: 'hello', age: 12}