import { IfNoDeepValue } from './helpers.js'; import { IfNever, IfNull, IfTuple } from './type-check.js'; /** * Exclude null and undefined from T deeply */ export type UnNullish = { [ K in keyof T as IfNever< Exclude, never, IfNull, never, K> > ]: NonNullable; }; /** * Exclude null and undefined from T deeply */ export type DeepUnNullish = { [ K in keyof T as IfNever< Exclude, never, IfNull, never, K> > ]: IfNoDeepValue< // Do not deep process No-Deep values Exclude > extends true ? NonNullable : // Deep process objects DeepUnNullish>; }; /** * Exclude null and undefined from T deeply including arrays */ export type DeeperUnNullish = { [ K in keyof T as IfNever< Exclude, never, IfNull, never, K> > ]: IfTuple> extends true // Deep process tuples positionally ? DeeperUnNullishTuple> : NonNullable> extends readonly (infer U)[] ? NonNullable extends any[] ? DeeperUnNullish[] // was mutable - UnNullish doesn't touch mutability : readonly DeeperUnNullish[] // was readonly - preserve that : // Do not deep process No-Deep values IfNoDeepValue> extends true ? NonNullable : // Deep process objects DeeperUnNullish>; }; type DeeperUnNullishTuple = T extends readonly [infer Head, ...infer Rest] ? [ IfNoDeepValue> extends true ? NonNullable : DeeperUnNullish>, ...DeeperUnNullishTuple, ] : [];