UNPKG

964 BTypeScriptView Raw
1import type {ConditionalKeys} from './conditional-keys';
2
3/**
4Pick keys from the shape that matches the given `Condition`.
5
6This is useful when you want to create a new type from a specific subset of an existing type. For example, you might want to pick all the primitive properties from a class and form a new automatically derived type.
7
8@example
9```
10import type {Primitive, ConditionalPick} from 'type-fest';
11
12class Awesome {
13 name: string;
14 successes: number;
15 failures: bigint;
16
17 run() {}
18}
19
20type PickPrimitivesFromAwesome = ConditionalPick<Awesome, Primitive>;
21//=> {name: string; successes: number; failures: bigint}
22```
23
24@example
25```
26import type {ConditionalPick} from 'type-fest';
27
28interface Example {
29 a: string;
30 b: string | number;
31 c: () => void;
32 d: {};
33}
34
35type StringKeysOnly = ConditionalPick<Example, string>;
36//=> {a: string}
37```
38
39@category Object
40*/
41export type ConditionalPick<Base, Condition> = Pick<
42Base,
43ConditionalKeys<Base, Condition>
44>;