import type {BuiltIns, HasMultipleCallSignatures} from './internal/type.d.ts'; import type {IsNever} from './is-never.d.ts'; import type {Simplify} from './simplify.d.ts'; /** Recursively removes `null` and `undefined` from the specified type. Use-cases: - Normalizing data received from external sources where `null`/`undefined` have been cleaned. - Creating non-nullable variants of deeply nested types. NOTE: Optional modifiers (`?`) are not removed from properties. For example, `NonNullableDeep<{foo?: string | null | undefined}>` will result in `{foo?: string}`. To remove both optional modifiers and nullables, use {@link RequiredDeep} in conjunction with this type. @example ``` import type {NonNullableDeep} from 'type-fest'; type UserDraft = { name: string | null; address: { city: string | undefined; postalCode: string | null; landmark?: string | undefined; }; tags: Array; visits: Map; }>; }; type User = NonNullableDeep; //=> { // name: string; // address: { // city: string; // postalCode: string; // landmark?: string; // }; // tags: string[]; // visits: Map; // }>; // } ``` @example ``` import type {NonNullableDeep} from 'type-fest'; type ArrayExample = NonNullableDeep<[{a: number | undefined}, ...Array<{b: string | null}>]>; //=> [{a: number}, ...{b: string}[]] type MapExample = NonNullableDeep<{a: Map<{a: string | null}, {c: number | undefined}>}>; //=> {a: Map<{a: string}, {c: number}>} type SetExample = NonNullableDeep | null | undefined>; //=> Set<{a: string}> type PromiseExample = NonNullableDeep<{a: Promise<{b: string | null}>}>; //=> {a: Promise<{b: string}>} type FunctionExample = NonNullableDeep<(a: string | null) => number | undefined>; //=> (a: string) => number ``` @category Utilities @category Object @category Array @category Set @category Map */ export type NonNullableDeep = T extends BuiltIns | (new (...arguments_: any[]) => unknown) ? Exclude // `Exclude` is used instead of `NonNullable` because `NonNullable` results in `void & {}`. : T extends Map ? Map, NonNullableDeep> : T extends Set ? Set> : T extends ReadonlyMap ? ReadonlyMap, NonNullableDeep> : T extends ReadonlySet ? ReadonlySet> : T extends WeakMap ? WeakMap, NonNullableDeep> : T extends WeakSet ? WeakSet> : T extends Promise ? Promise> : T extends (...arguments_: any[]) => unknown ? HasMultipleCallSignatures extends true ? T : ((...arguments_: NonNullableDeep>) => NonNullableDeep>) & (IsNever extends true ? unknown : NonNullableDeep>) // `Simplify` removes the call signature : T extends object ? {[P in keyof T]: NonNullableDeep} : unknown; export {};