2 Extract the keys from a type where the value type of the key extends the given `Condition`.
4 Internally this is used for the `ConditionalPick` and `ConditionalExcept` types.
8 import {ConditionalKeys} from 'type-fest';
17 type StringKeysOnly = ConditionalKeys<Example, string>;
21 To support partial types, make sure your `Condition` is a union of undefined (for example, `string | undefined`) as demonstrated below.
25 type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
29 export type ConditionalKeys<Base, Condition> = NonNullable<
30 // Wrap in `NonNullable` to strip away the `undefined` type from the produced union.
32 // Map through all the keys of the given base type.
34 // Pick only keys with types extending the given `Condition` type.
35 Base[Key] extends Condition
36 // Retain this key since the condition passes.
38 // Discard this key since the condition fails.
41 // Convert the produced object into a union type of the keys which passed the conditional test.