ObjectEntries
介绍
从类型T中, 筛选属性类型不能赋值为'U'的属性集合.
ts
interface Model {name: string;age: number;locations: string[] | null;}type modelEntries = ObjectEntries<Model> // ['name', string] | ['age', number] | ['locations', string[] | null];
View on GitHubts
interface Model {name: string;age: number;locations: string[] | null;}type modelEntries = ObjectEntries<Model> // ['name', string] | ['age', number] | ['locations', string[] | null];
起点
ts
/* _____________ Your Code Here _____________ */typeObjectEntries <T > = any/* _____________ Test Cases _____________ */interfaceModel {name : stringage : numberlocations : string[] | null}typeModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null]typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <Model >,ModelEntries >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <Partial <Model >>,ModelEntries >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <{key ?: undefined }>, ['key', undefined]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <{key : undefined }>, ['key', undefined]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <{key : string | undefined }>, ['key', string | undefined]>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeObjectEntries <T > = any/* _____________ Test Cases _____________ */interfaceModel {name : stringage : numberlocations : string[] | null}typeModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null]typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <Model >,ModelEntries >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <Partial <Model >>,ModelEntries >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <{key ?: undefined }>, ['key', undefined]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <{key : undefined }>, ['key', undefined]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ObjectEntries <{key : string | undefined }>, ['key', string | undefined]>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
/*** Required<T> 会将可选属性的值undefined变成never*/// most populartypeObjectEntries <T ,U =Required <T >> = {[K in keyofU ]:[K ,U [K ]]}[keyofU ]// 因为要解决值变成never的问题, 所以多了个条件判断typeObjectEntries2 <T ,U =Required <T >> = {[K in keyofU ]: [K ,U [K ] extends never ? undefined :U [K ]]}[keyofU ]
ts
/*** Required<T> 会将可选属性的值undefined变成never*/// most populartypeObjectEntries <T ,U =Required <T >> = {[K in keyofU ]:[K ,U [K ]]}[keyofU ]// 因为要解决值变成never的问题, 所以多了个条件判断typeObjectEntries2 <T ,U =Required <T >> = {[K in keyofU ]: [K ,U [K ] extends never ? undefined :U [K ]]}[keyofU ]