List

Extends the built-in Array object with additional convenience methods derived from the Arrays module.

Constructor

public constructor(listLength: number): T[];
public constructor(...items: T[]): T[];

Creates a list with either a fixed size or from the specified elements.

Mimics the Array constructor.

Public Non-Mutator Methods

public get(index: number): T

Gets the item at the specified index.

Unlike bracket syntax (this[index]), this function throws an error if there is no element at the given index, meaning that the return value of this function can never be undefined (unless undefined is part of T).

Remarks

See Arrays for all functions contained within this class.

Note that, while using List<T> is quite convenient and makes for more readable syntax than the Arrays module’s free functions, it also commits the entire Arrays module to your code, potentially bloating your bundle size. Therefore it is recommended only to use List<T> if size is not a concern.

There is also a read-only interface of List<T> available called ReadonlyList<T>.

To create a new List<T> from a given Array<T>, you can either spread the array’s items into the list constructor or you can use one of the available static functions:

const array = [0, 1, 2];
const list1 = List.from(array);   // preferred
const list2 = new List(...array);
const list3 = List.of(...array);