Type Alias PartialOnUndefinedDeep<T, Options>

PartialOnUndefinedDeep: T extends Record<any, any>
| undefined
    ? {
        [KeyType in keyof T as undefined extends T[KeyType] ? KeyType : never]?: PartialOnUndefinedDeepValue<
            T[KeyType],
            Options,
        >
    } extends infer U
        ? Merge<
            {
                [KeyType in keyof T as KeyType extends keyof U ? never : KeyType]: PartialOnUndefinedDeepValue<
                    T[KeyType],
                    Options,
                >
            },
            U,
        >
        : never
    : T

Create a deep version of another type where all keys accepting undefined type are set to optional.

This utility type is recursive, transforming at any level deep. By default, it does not affect arrays and tuples items unless you explicitly pass {recurseIntoArrays: true} as the second type argument.

Use-cases:

  • Make all properties of a type that can be undefined optional to not have to specify keys with undefined value.

Type Parameters

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

interface Settings {
optionA: string;
optionB: number | undefined;
subOption: {
subOptionA: boolean;
subOptionB: boolean | undefined;
}
};

const testSettings: PartialOnUndefinedDeep<Settings> = {
optionA: 'foo',
// 👉 optionB is now optional and can be omitted
subOption: {
subOptionA: true,
// 👉 subOptionB is now optional as well and can be omitted
},
};