Partial

将 type / interface 中每一个选项变成可选的。

Required

将 type / interface 中每一个选项变成必选的。

ReadOnly

将 type / interface 中每一个选项变成只读的。

Record

将Type 中的 key 作为类型的键,type 为类型的值,生成新的类型

  1. interface Person {
  2. name:string,
  3. age:number
  4. }
  5. type qujunProps = Record<"qujun",Person>
  6. function getInfo(info:qujunProps){
  7. return info
  8. }
  9. getInfo({
  10. "qujun":{
  11. age:18,
  12. name:'hi'
  13. }
  14. })

Pick

  1. type 中指定属性,生成一个新的type

type Pick<T, K extends keyof T> = { [P in K]: T[P]; }

  1. interface Person {
  2. name:string,
  3. age:number
  4. }
  5. type Pick2<T, K extends keyof T> = { [P in K]: T[P]; }
  6. type OnlyAge = Pick2<Person,"age">

Omit

在type 中移除指定的属性,生成一个新的 type

type Omit<T, K extends string | number | symbol> = {[P in Exclude<keyof T, K>]:T[P];}

Exclude

在联合类型中移除指定的类型值

type Exclude<T,P> = P extends T ? never: P

Extract

取两个类型的交集生成一个新的类型

type Extract<T,P> = T extends P ? T : never

NotNullable

移除联合类型中 undefined 和 null 的值

type NonNullable1<T> = T extends undefined|null ? never : T

Parame

将一个函数类型的type的参数类型返回成一个元组
type Parameters<Textends (...args:any) => any> = Textends (...args:inferP) => any ? P :never;
问题:如何转成对象形式的 type 比如 T1 转成 :{s:string, k:number}

  1. type T0 = Parameters<() => string>; // T0 = []
  2. type T1 = Parameters1<(s: string, k:number) => void>; T1= [s:string,k:number]