Exclude
介绍
Implement the built-in Exclude<T, U>
View on GitHubExclude from T those types that are assignable to U
起点
ts
/* _____________ Your Code Here _____________ */typeMyExclude <T ,U > = any;/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyExclude <'a' | 'b' | 'c', 'a'>,Exclude <'a' | 'b' | 'c', 'a'>>>,Expect <Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Equal <MyExclude <'a' | 'b' | 'c', 'a' | 'b'>,Exclude <'a' | 'b' | 'c', 'a' | 'b'>>>,Expect <Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Equal <MyExclude <string | number | (() => void),Function >,Exclude <string | number | (() => void),Function >>>];
take the challengets
/* _____________ Your Code Here _____________ */typeMyExclude <T ,U > = any;/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyExclude <'a' | 'b' | 'c', 'a'>,Exclude <'a' | 'b' | 'c', 'a'>>>,Expect <Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Equal <MyExclude <'a' | 'b' | 'c', 'a' | 'b'>,Exclude <'a' | 'b' | 'c', 'a' | 'b'>>>,Expect <Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Equal <MyExclude <string | number | (() => void),Function >,Exclude <string | number | (() => void),Function >>>];
解决方案
Spoiler warning // Click to reveal answer
ts
typeMyExclude <T ,U > =T extendsU ? never :T ;
ts
typeMyExclude <T ,U > =T extendsU ? never :T ;
ts
typeMyExclude <T ,U > = ((t :T extendsU ? never :T ) => void) extends ((t : inferR ) => void) ?R : never;
ts
typeMyExclude <T ,U > = ((t :T extendsU ? never :T ) => void) extends ((t : inferR ) => void) ?R : never;