Typescript类型体操 - Exclude

题目

中文

实现内置的Exclude <T, U>类型,但不能直接使用它本身。

从联合类型T中排除U的类型成员,来构造一个新的类型。

例如:

type Result = MyExclude<'a' | 'b' | 'c', 'a'> // 'b' | 'c'

English

Implement the built-in Exclude<T, U>

Exclude from T those types that are assignable to U

For example:

type Result = MyExclude<'a' | 'b' | 'c', 'a'> // 'b' | 'c'

答案

type MyExclude<T, U> = T extends U ? never : T;

在线演示

posted @ 2022-09-04 19:47  Laggage  阅读(92)  评论(0编辑  收藏  举报