Typescript类型体操 - PickByType
题目
中文
找出T
中类型为U
的属性
示例:
type OnlyBoolean = PickByType<
{
name: string;
count: number;
isReadonly: boolean;
isEnable: boolean;
},
boolean
>; // { isReadonly: boolean; isEnable: boolean; }
English
From T
, pick a set of properties whose type are assignable to U
.
For Example
type OnlyBoolean = PickByType<
{
name: string;
count: number;
isReadonly: boolean;
isEnable: boolean;
},
boolean
>; // { isReadonly: boolean; isEnable: boolean; }
答案
type PickByType<T, U> = { [P in keyof T as T[P] extends U ? P : never]: T[P] };