Skip to main content

compactObject

compactObject() function

Returns an object with all nullable values removed.

Signature:

export default function compactObject<T extends object>(obj: T): Merge<{
[K in Extract<keyof T, RequiredKeys<T>>]: Extract<NonNullableEntries<T>, [
K,
any
]>[1];
}, {
[K in Extract<keyof T, OptionalKeys<T>>]?: Extract<NonNullableEntries<T>, [
K,
any
]>[1];
}>;

Returns:

Merge<{ [K in Extract<keyof T, RequiredKeys<T>>]: Extract<NonNullableEntries<T>, [ K, any ]>[1]; }, { [K in Extract<keyof T, OptionalKeys<T>>]?: Extract<NonNullableEntries<T>, [ K, any ]>[1]; }>

Example

const compacted = compactObject({ a: 1, b: "b", c: null, d: undefined });
// {a: 1, b: "b"}