一个类型需要基于另外一个类型,但是你又不想拷贝一份,这个时候可以考虑使用映射类型
映射类型建立在索引签名的语法上
1 2 3
| type OptionsFlags<Type> = { [Property in keyof Type]: boolean; };
|
映射修饰符
readonly 属性只读
? 属性可选
通过前缀+-添加或删除修饰符,默认为+前缀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| type CreateMutable<Type> = { -readonly [Property in keyof Type]: Type[Property]; }; type LockedAccount = { readonly id: string; readonly name: string; }; type UnlockedAccount = CreateMutable<LockedAccount>;
type Concrete<Type> = { [Property in keyof Type]-?: Type[Property]; };
|
通过 as 实现键名重新映射
在映射类型中使用 as 语句实现键名重新映射
1 2 3
| type MappedTypeWithNewProperties<Type> = { [Properties in keyof Type as NewKeyType]: Type[Properties] }
|
可以利用模板字面量类型基于之前的属性名创建一个全新的属性名
1 2 3 4 5 6 7 8 9 10 11 12 13
| type Getters<Type> = { [Property in keyof Type as `get${Capitalize< string & Property >}`]: () => Type[Property]; };
interface Person { name: string; age: number; location: string; }
type LazyPerson = Getters<Person>;
|
可以遍历任何联合类型
1 2 3 4 5 6 7 8 9 10 11 12
| type EventConfig<Events extends { kind: string }> = { [E in Events as E["kind"]]: (event: E) => void; } type SquareEvent = { kind: "square", x: number, y: number }; type CircleEvent = { kind: "circle", radius: number }; type Config = EventConfig<SquareEvent | CircleEvent>
|