跳到主要内容

Exclude

介绍

Implement the built-in Exclude<T, U>

Exclude from T those types that are assignable to U

View on GitHub

起点

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>
>
>
];
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>
>
>
];
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
type MyExclude<T, U> = T extends U ? never : T;
ts
type MyExclude<T, U> = T extends U ? never : T;
ts
type MyExclude<T,U> = ((t: T extends U ? never : T) => void) extends ((t: infer R) => void) ? R : never;
ts
type MyExclude<T,U> = ((t: T extends U ? never : T) => void) extends ((t: infer R) => void) ? R : never;
view more solutions

知识点