Skip to content

Latest commit

 

History

History
85 lines (58 loc) · 936 Bytes

File metadata and controls

85 lines (58 loc) · 936 Bytes

typelab / utils / ObjectPick

type ObjectPick<T, K, BailType> = { [Key in K as PropValue<T, Key> extends never ? never : Key]: PropValue<T, Key> } extends infer Result ? IsNonNullish<Result> extends true ? BailType : Result : never;

Extended TypeScript `Pick` to pick K from T.

Type Parameters

Type Parameter Default type Description

T

The target type.

K extends keyof T

The keys of T that will be picked.

BailType

never

Returns

Set of properties whose keys are in the union K.

Example

type Obj = { a: string; b: string };

// { a: string; }
type Picked = ObjectPick<Obj, 'a'>;