type NonUndefined = T extends undefined ? never : T type NonNull = T extends null ? never : T type NonNullableObject = { [K in keyof T]: NonUndefined> } type Prettify = { [K in keyof T]: T[K] } & {} export const stripUndefinedOrNull = (obj: T): Prettify> => { const strip = (input: unknown): unknown => { return Array.isArray(input) ? input.map(strip) : input !== null && typeof input === 'object' ? Object.entries(input) .filter(([, value]) => value !== undefined && value !== null) .reduce((acc, [key, value]) => { acc[key as keyof typeof acc] = strip(value) return acc }, {} as Record) : input } return strip(obj) as Prettify> }