Skip to content

Latest commit

 

History

History
73 lines (50 loc) · 1.23 KB

File metadata and controls

73 lines (50 loc) · 1.23 KB

typelab / utils / ObjectOmitRequired

type ObjectOmitRequired<T, Z> = IsObjectLiteral<T> extends true ? { [K in keyof T as IsOptionalProperty<T, K> extends true ? K : never]?: _Lookup<Z, { shallow: T[K]; deep: ObjectOmitRequired<ExcludeUndefined<T[K]>> }> } : T;

Get the optional properties from T type.

This type will lookup all nested objects (if Z is 'deep').

Type Parameters

Type Parameter Default type Description

T

The object type where optional properties will be identified.

Z extends _LookupType

"shallow"

Defines the lookup type, which can be 'deep' or 'shallow', defaults to 'shallow'.

Returns

A type with only the optional properties from T, optionally nested based on Z.

Example

type Obj = { a: string | undefined; b?: string; c?: { d: string; e?: string } };

// { b?: string; c?: { d: string; e?: string; }; }
type Shallow = ObjectOmitRequired<Obj, 'shallow'>;

// { b?: string; c?: { e?: string; }; }
type Deep = ObjectOmitRequired<Obj, 'deep'>;