一个类型需要基于另外一个类型,但是又不想拷贝一份,可以考虑用映射类型
映射修饰符:
在使用映射类型时,两个额外修饰符:readonly用于设置属性只读,?用于设置属性可选,可以通过前缀-或者+删除或者添加这些修饰符,如果没有写前缀,相当于使用了+前缀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
   |  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]; } type MaybeUser={     id:string;     name?:string;     age?:number; } type User=Concrete<MaybeUser>;
 
 
 
 
 
 
  | 
 
as实现键名重新映射
1 2 3
   | type MappedTypeWithProperties<Type>={     [Properties in keyof Type as NewKeyType]:Type[Properties] }
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | 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>;
 
 
 
 
 
 
  | 
 
参考:
https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html