Type Alias ConditionalKeys<Base, Condition>

ConditionalKeys: NonNullable<
    { [Key in keyof Base]: Base[Key] extends Condition ? Key : never }[keyof Base],
>

Extract the keys from a type where the value type of the key extends the given Condition.

Internally this is used for the ConditionalPick and ConditionalExcept types.

Type Parameters

  • Base
  • Condition
import type {ConditionalKeys} from 'type-fest';

interface Example {
a: string;
b: string | number;
c?: string;
d: {};
}

type StringKeysOnly = ConditionalKeys<Example, string>;
//=> 'a'

To support partial types, make sure your Condition is a union of undefined (for example, string | undefined) as demonstrated below.

import type {ConditionalKeys} from 'type-fest';

type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
//=> 'a' | 'c'