All files / types pick-by-type.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21                                         
/**
 * Lists the keys of the properties whose type matches: the numeric fields of a record to chart, the
 * boolean flags of a settings object.
 *
 * @template T - The object type.
 * @template V - The property type to look for.
 * @example
 * type NumericField = KeysOfType<Sample, number>; // 'speed' | 'heading'
 */
export type KeysOfType<T, V> = { [K in keyof T]-?: T[K] extends V ? K : never }[keyof T];
 
/**
 * Keeps the properties whose type matches.
 *
 * @template T - The object type.
 * @template V - The property type to keep.
 * @example
 * type Flags = PickByType<Settings, boolean>;
 */
export type PickByType<T, V> = Pick<T, KeysOfType<T, V>>;