多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

26 lines
815 B

type NonUndefined<T> = T extends undefined ? never : T
type NonNull<T> = T extends null ? never : T
type NonNullableObject<T> = {
[K in keyof T]: NonUndefined<NonNull<T[K]>>
}
type Prettify<T> = {
[K in keyof T]: T[K]
} & {}
export const stripUndefinedOrNull = <T>(obj: T): Prettify<NonNullableObject<T>> => {
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<string, unknown>)
: input
}
return strip(obj) as Prettify<NonNullableObject<T>>
}