import { isObject } from "./is-object" export function pruneNullishValues(input: T): PruneNullishValues { if (Array.isArray(input)) { return input.filter(Boolean).map((item) => pruneNullishValues(item)) as any } if (!isObject(input)) { return input as any } const result: any = {} for (const [key, value] of Object.entries(input as any)) { if (value != undefined) { result[key] = pruneNullishValues(value) } } return result } 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]