Type Alias ConditionalPickDeep<Type, Condition, Options>

ConditionalPickDeep: ConditionalSimplifyDeep<
    ConditionalExcept<
        {
            [Key in keyof Type]: AssertCondition<Type[Key], Condition, Options> extends true
                ? Type[Key]
                : Type[Key] extends object
                    ? ConditionalPickDeep<Type[Key], Condition, Options>
                    : ConditionalPickDeepSymbol
        },
        ConditionalPickDeepSymbol | undefined
        | Record<PropertyKey, never>,
    >,
>

Pick keys recursively from the shape that matches the given condition.

Type Parameters

ConditionalPick

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

interface Example {
a: string;
b: string | boolean;
c: {
d: string;
e: {
f?: string;
g?: boolean;
h: string | boolean;
i: boolean | bigint;
};
j: boolean;
};
}

type StringPick = ConditionalPickDeep<Example, string>;
//=> {a: string; c: {d: string}}

type StringPickOptional = ConditionalPickDeep<Example, string | undefined>;
//=> {a: string; c: {d: string; e: {f?: string}}}

type StringPickOptionalOnly = ConditionalPickDeep<Example, string | undefined, {condition: 'equality'}>;
//=> {c: {e: {f?: string}}}

type BooleanPick = ConditionalPickDeep<Example, boolean | undefined>;
//=> {c: {e: {g?: boolean}; j: boolean}}

type NumberPick = ConditionalPickDeep<Example, number>;
//=> {}

type StringOrBooleanPick = ConditionalPickDeep<Example, string | boolean>;
//=> {
// a: string;
// b: string | boolean;
// c: {
// d: string;
// e: {
// h: string | boolean
// };
// j: boolean;
// };
// }

type StringOrBooleanPickOnly = ConditionalPickDeep<Example, string | boolean, {condition: 'equality'}>;
//=> {b: string | boolean; c: {e: {h: string | boolean}}}