Skip to content

Latest commit

 

History

History
85 lines (59 loc) · 1.27 KB

File metadata and controls

85 lines (59 loc) · 1.27 KB

typelab / utils / ArrayUnionize

type ArrayUnionize<Target, UnionType, Z> = IsArray<Target> extends true ? _ArrayUnionize<Target, UnionType, Z> : never;

Unions the elements of an Array with a specified UnionType, creating a new Array where each element is either the original element or the UnionType.

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

Type Parameters

Type Parameter Default type Description

Target extends ReadonlyArray

The Array to be unionized.

UnionType

The type to union with each element of the Target.

Z extends _LookupType

"shallow"

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

Example

// [string | number, number | [number, boolean]]
type Shallow = ArrayUnionize<[string, [number, boolean]], number, 'shallow'>;

// [string | number, [number, number | boolean]]
type Deep = ArrayUnionize<[string, [number, boolean]], number, 'deep'>;