内置类型体操
1.Pink
type MyPick <T extends object,K extends keyof T> = {
[Key in K] : T[Key]
}
interface Todo{
title:string
description:string
completed:boolean
}
type TodoPreview = MyPick<Todo, 'title'|'completed'>
const todo:TodoPreview = {
title:"Clean room",
completed:false
}
K extends keyof T:表示K只能是keyof T的子类型,如果我们在使用Pick的时候传递了不存在于T的字段,会报错:
2.Readonly
type MyReadonly<T extends object>={
readonly [key in keyof T] : T[key]
}
interface Todo2 {
title:string
description:string
}
const todo2 : MyReadonly<Todo2>={
title:"Hey",
description:"foobar"
}
3.Exclude
type ExcludeResult = Exclude<'name'|'age'|'sex', 'sex'|'address'>
type MyExclude<T,U> = T extends U ? never:T
- T extends U : 从 T 的子类型开始分发
T extends U
=> 'name'|'age'|'sex' extends 'sex'|'address'
=> (
'name' extends 'sex'|'address' ? never : 'name' |
'age' extends 'sex'|'address' ? never : 'age' |
'sex' extends 'sex'|'address' ? never : 'sex'
)
=> 'name'|'age'
4.Parameters(函数的参数类型)
const add = (a:number,b:string):void=>{}
type result = MyParameters<typeof add>
type MyParameters<T extends (...args:any[])=>any> = T extends (...args:infer R)=>any?R:never
5.Partial(所有属性可填)
type Person = {
name:string;
age?:number;
}
type PartialResult = MyPartial<Person>
type MyPartial<T>={
[P in keyof T]?:T[P]
}
6.Required(所有属性必填)
type Person = {
name:string;
age?:number;
}
type RequiredResult = MyRequired<Person>
type MyRequired<T>={
[P in keyof T]:T[P]
}
自定义泛型工具类型
7.元组转换为对象
confst tuple = ['tesla','model 3','model X','model Y'] as const
type result = TupleToObject<typeof tuple>
type TupleToObject<T extends readonly any[]> = {
[K in T[number]] = K
}
tesla: 'tesla',
'model 3': 'model 3',
'model X': 'model X',
'model Y': 'model Y'
}
8.# 取出数组第一个元素
type arr1 = ['a','b','c']
type arr2 = [3,2,1]
type head1 = First<arr1>
type head2 = First<arr2>
type First<T extends any[]>=T['length'] extends 0?never:T[0]
9.获取元组长度
type tesla = ['tesla', 'model 3', 'model X', 'model Y']
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']
type teslaLength = Length<tesla>
type spaceXLength = Length<spaceX>
type Length<T extends readonly any[]> = T['length']