Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 855 Bytes

File metadata and controls

50 lines (36 loc) · 855 Bytes

typelab / utils / ArrayFlat

type ArrayFlat<T> = T extends ReadonlyArray ? _IfNotAnyOrNever<T, _ArrayFlat<T>> : never;

Type that flattens a nested Array structure into a single Array. If an element is an Array, it will recursively flatten it until no Array remains.

Type Parameters

Type Parameter Description

T

The Array type to flatten.

Returns

A flattened version of the given Array type.

Example

// [0, 1, 2, 3, 4, 5, 6, 7]
type Flattened1 = ArrayFlat<[0, 1, [2, 3, [4, 5, [6, 7[]]]]]>;

// (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7)[]
type Flattened2 = ArrayFlat<(0 | 1 | [2] | [3] | [[4]] | [[5]] | [[[6]]] | [[[7[]]]])[]>;

// never
type Never = ArrayFlat<any>;