Skip to content

Latest commit

 

History

History
73 lines (50 loc) · 1.22 KB

File metadata and controls

73 lines (50 loc) · 1.22 KB

typelab / utils / ObjectOmitOptional

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

Get the required 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 required 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 required properties from T, optionally nested based on Z.

Example

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

// { a: string | undefined; c: { d: string; e?: string } }
type Shallow = ObjectOmitOptional<Obj, 'shallow'>;

// { a: string | undefined; c: { d: string; }}
type Deep = ObjectOmitOptional<Obj, 'deep'>;