Skip to content

Latest commit

 

History

History
85 lines (59 loc) · 1.33 KB

File metadata and controls

85 lines (59 loc) · 1.33 KB

typelab / utils / ObjectUnionize

type ObjectUnionize<Target, UnionType, Z> = IsObjectLiteral<Target> extends true ? { [K in keyof Target]: _Lookup<Z, { shallow: Target[K] | UnionType; deep: _ObjectUnionizeDeep<Target[K], UnionType> }> } : never;

Unions the properties of an object with a specified UnionType, creating a new object where each property is either the original value or the UnionType.

This type will unionize all nested object types (if Z is 'deep').

Type Parameters

Type Parameter Default type Description

Target

The object to be unionized.

UnionType

The type to union with each property of the Target.

Z extends _LookupType

"shallow"

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

Example

// { a: string | number; b: number | { a: string; }; }
type Shallow = ObjectUnionize<{ a: string; b: { a: string } }, number, 'shallow'>;

// { a: string | number; b: { a: string | number; }; }
type Deep = ObjectUnionize<{ a: string; b: { a: string } }, number, 'deep'>;