Typescript类型体操 - OmitByType
题目
中文
从T
中选出一组无法分配给U
的属性
示例:
type OmitBoolean = OmitByType<
{
name: string;
count: number;
isReadonly: boolean;
isEnable: boolean;
},
boolean
>; // { name: string; count: number }
English
From T
, pick a set of properties whose type are not assignable to U
.
For Example
type OmitBoolean = OmitByType<
{
name: string;
count: number;
isReadonly: boolean;
isEnable: boolean;
},
boolean
>; // { name: string; count: number }
答案
type OmitByType<T, U> = { [K in keyof T as T[K] extends U ? never : K]: T[K] };