Skip to content

Latest commit

 

History

History
57 lines (41 loc) · 871 Bytes

File metadata and controls

57 lines (41 loc) · 871 Bytes

typelab / utils / ArrayPush

type ArrayPush<T, U> = T extends WritableArray ? _IfNotAnyOrNever<T, IfNever<U, T, [...T, U]>> : never;

Adds a new element to the end of a Writable Array type.

Type Parameters

Type Parameter Description

T

The Array type to push into.

U

The element type to add to the Array.

Returns

A new Array type with the element U added to the end.

Example

type Pushed = ArrayPush<[string, number], boolean>; // [string, number, boolean]
type Same = ArrayPush<[string, number], never>; // [string, number]
type Never1 = ArrayPush<readonly [string, number], string> // never
type Never2 = ArrayPush<any, string> // never