OmitByType
介绍
从类型T中, 筛选属性类型不能赋值为'U'的属性集合.
ts
type OmitBoolean = OmitByType<{name: stringcount: numberisReadonly: booleanisEnable: boolean}, boolean> // { name: string; count: number }
View on GitHubts
type OmitBoolean = OmitByType<{name: stringcount: numberisReadonly: booleanisEnable: boolean}, boolean> // { name: string; count: number }
起点
ts
/* _____________ Your Code Here _____________ */typeOmitByType <T ,U > = any/* _____________ Test Cases _____________ */interfaceModel {name : stringcount : numberisReadonly : booleanisEnable : boolean}typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <OmitByType <Model , boolean>, {name : string,count : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <OmitByType <Model , string>, {count : number,isReadonly : boolean,isEnable : boolean }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <OmitByType <Model , number>, {name : string,isReadonly : boolean,isEnable : boolean }>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeOmitByType <T ,U > = any/* _____________ Test Cases _____________ */interfaceModel {name : stringcount : numberisReadonly : booleanisEnable : boolean}typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <OmitByType <Model , boolean>, {name : string,count : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <OmitByType <Model , string>, {count : number,isReadonly : boolean,isEnable : boolean }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <OmitByType <Model , number>, {name : string,isReadonly : boolean,isEnable : boolean }>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeOmitByType <T ,U > = {[P in keyofT asT [P ] extendsU ? never :P ]:T [P ]}
ts
typeOmitByType <T ,U > = {[P in keyofT asT [P ] extendsU ? never :P ]:T [P ]}