import { isObject } from "./is-object" export function pruneNullishValues(input: T): PruneNullishValues { if (!isObject(input)) { return input as PruneNullishValues } if (Array.isArray(input)) { return input .filter(Boolean) .map((item) => pruneNullishValues(item)) as PruneNullishValues } const result: Record = {} for (const [key, value] of Object.entries(input)) { if (value != undefined) { result[key] = pruneNullishValues(value) } } return result as PruneNullishValues } export type PruneNullishValues = Input extends object ? OptionalKeys< { [Key in keyof Input]: NonNullable> }, KeysWithNullishValues > : Input type OptionalKeys = Omit & { [Key in Keys]?: Input[Key] } type KeysWithNullishValues = NonNullable< Values<{ [Key in keyof Input]: null extends Input[Key] ? Key : undefined extends Input[Key] ? Key : never }> > type Values = Input[keyof Input]