- 实现 Partial:
typeA={name:string,age:string}
type Partial<T>={
[p in keyof T]?:T[p]
}
//即 type C=Partial<A>
- 实现 pick 提取部分类型
type example={name:string,age:number,address:string}
type Pick<T,U extends keyof T>={
[P in U]=T[P]
}
const tiqu:Pick<example,"name"|"age">={}
- 实现 Extract
type Extract<T,U>=T extends U? T: never
type A=string | number | boolean
type B=string | number
type C=Extract<A,B>
const result:C="214214"
const result2:C=444
- 实现 Exclude,与 Extract 思路一样
type Exclude<T,U>=T extends U?never :T
type A=string | number | boolean
type B=string | number
type C=Exclude<A,B>
const result:C="214214" //错误
const result2:C=true
- 实现 Record
type Record<K extends string|number|symbol,V>={
[P in K]:V
}
type EX=Record<'name'|'age',string|number>
const res:EX={name:"张三",age:999}