夺宝网站制作,建设商城网站的难点,wordpress菜单显示图片,郑州市网站和公众号建设使用泛型来做类型映射#xff0c;将对象(或数组)中类型转换为另一个类型
首先#xff0c;定义一个类型Student // 定义一个类型Studentinterface Student {name: string,age: number}1、把Student的所有属性都变为可空的 type NullableT {[p in keyof T]: T[p] || …使用泛型来做类型映射将对象(或数组)中类型转换为另一个类型
首先定义一个类型Student // 定义一个类型Studentinterface Student {name: string,age: number}1、把Student的所有属性都变为可空的 type NullableT {[p in keyof T]: T[p] || null}type NullAbleStudent NullableStudent2、把Student的所有属性都变为只读的 type ReadonlyT {readonly [P in keyof T]: T[p]}type ReadonlyStudent ReadonlyStudent3、把Student的属性都变成可选的 type ParticalT {[P in keyof T]?: T[P] }type ParticalStudent ParticalStudent把属性都变成Promise type ToPromiseT {[P in keyof T]: PromiseT[P]}type Coordinate [number, number]type PromiseCoordinate ToPromiseCoordinate // [Promisenumber, Promisenumber]function toRefsT extends object (object: T): {[K in keyof T]: ToRefT[K]
}function toRefsT extends object 这是一个泛型函数它接受一个类型为 object 的参数 object并且返回一个新的对象。: { [K in keyof T]: ToRefT[K] } 这是函数的返回类型
它使用了 TypeScript 中的映射类型遍历了输入对象 T 的所有属性并将它们的类型转换为 ToRefT[K]
这里的 ToRef 是一个类型它表示将普通值转换为 ref 对象的类型。