Skip to main content

Exclude

introduction#

Implement the built-in Exclude<T, U>

Exclude from T those types that are assignable to U

View on GitHub

start point#

ts
/* _____________ Your Code Here _____________ */
type MyExclude<T, U> = any;
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<MyExclude<'a' | 'b' | 'c', 'a'>, Exclude<'a' | 'b' | 'c', 'a'>>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<
Equal<MyExclude<'a' | 'b' | 'c', 'a' | 'b'>, Exclude<'a' | 'b' | 'c', 'a' | 'b'>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
>,
Expect<
Equal<
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
MyExclude<string | number | (() => void), Function>,
Exclude<string | number | (() => void), Function>
>
>
];
Try
ts
/* _____________ Your Code Here _____________ */
type MyExclude<T, U> = any;
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<MyExclude<'a' | 'b' | 'c', 'a'>, Exclude<'a' | 'b' | 'c', 'a'>>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<
Equal<MyExclude<'a' | 'b' | 'c', 'a' | 'b'>, Exclude<'a' | 'b' | 'c', 'a' | 'b'>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
>,
Expect<
Equal<
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
MyExclude<string | number | (() => void), Function>,
Exclude<string | number | (() => void), Function>
>
>
];
Try
take the challenge

my solution#

Spoiler warning // Click to reveal answer
ts
type MyExclude<T, U> = T extends U ? never : T;
Try
ts
type MyExclude<T, U> = T extends U ? never : T;
Try
view more solutions