题目
中文
找出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] };
标签:Typescript,isEnable,isReadonly,boolean,体操,PickByType,type
From: https://www.cnblogs.com/laggage/p/type-challenge-pick-by-type.html