A reactive array class.

The array interface here works similar to the builtin Array (i.e. T[]). The major difference is that one must use the get and set methods instead of using square brackets, i.e. use array.get(i) instead of array[i] and array.set(i, v) instead of array[i] = v. Not all builtin array methods are implemented right now, but most of them are.

Reads and writes to this array are reactive.

interface ReactiveArray<T> {
    length: number;
    at(index: number): undefined | T;
    concat(...values: T[]): ReactiveArray<T>;
    concat(...values: (T | ReadonlyReactiveArray<T> | T[])[]): ReactiveArray<T>;
    entries(): IterableIterator<[index: number, value: T]>;
    every(predicate: (value: T, index: number) => boolean): boolean;
    filter<U>(
        predicate: (value: T, index: number) => value is U,
    ): ReactiveArray<U>;
    filter(predicate: (value: T, index: number) => boolean): ReactiveArray<T>;
    find<U>(predicate: (value: T, index: number) => value is U): undefined | U;
    find(predicate: (value: T, index: number) => boolean): undefined | T;
    findIndex(predicate: (value: T, index: number) => boolean): number;
    findLast<U>(
        predicate: (value: T, index: number) => value is U,
    ): undefined | U;
    findLast(predicate: (value: T, index: number) => boolean): undefined | T;
    findLastIndex(predicate: (value: T, index: number) => boolean): number;
    flatMap<U>(
        callback: (value: T, index: number) => U | readonly U[],
    ): ReactiveArray<U>;
    forEach(callback: (value: T, index: number) => void): void;
    get(index: number): undefined | T;
    getItems(): T[];
    includes(value: T, fromIndex?: number): boolean;
    indexOf(value: T, fromIndex?: number): number;
    keys(): IterableIterator<number>;
    lastIndexOf(value: T): number;
    map<U>(callback: (value: T, index: number) => U): ReactiveArray<U>;
    pop(): undefined | T;
    push(...values: T[]): void;
    reduce(
        callback: (
            previousValue: T,
            currentValue: T,
            currentIndex: number,
        ) => T,
    ): T;
    reduce(
        callback: (
            previousValue: T,
            currentValue: T,
            currentIndex: number,
        ) => T,
        initialValue: T,
    ): T;
    reduce<U>(
        callback: (
            previousValue: U,
            currentValue: T,
            currentIndex: number,
        ) => U,
        initialValue: U,
    ): U;
    reduceRight(
        callback: (
            previousValue: T,
            currentValue: T,
            currentIndex: number,
        ) => T,
    ): T;
    reduceRight(
        callback: (
            previousValue: T,
            currentValue: T,
            currentIndex: number,
        ) => T,
        initialValue: T,
    ): T;
    reduceRight<U>(
        callback: (
            previousValue: U,
            currentValue: T,
            currentIndex: number,
        ) => U,
        initialValue: U,
    ): U;
    set(index: number, value: T): void;
    shift(): undefined | T;
    slice(start?: number): ReactiveArray<T>;
    slice(start: number, end?: number): ReactiveArray<T>;
    some(predicate: (value: T, index: number) => boolean): boolean;
    sort(compare: (a: T, b: T) => number): void;
    splice(start: number, deleteCount?: number): T[];
    splice(start: number, deleteCount: number, ...values: T[]): T[];
    unshift(...values: T[]): void;
    values(): IterableIterator<T>;
}

Type Parameters

  • T

Hierarchy (View Summary)

Methods

  • Returns the item at the given index, or undefined if the index is out of bounds. You can use negative indices to address items starting from the end of the array.

    See also Array.at.

    Parameters

    • index: number

    Returns undefined | T

  • Returns true if the predicate is satisfied for every item in this array, false otherwise.

    See also Array.every.

    Parameters

    • predicate: (value: T, index: number) => boolean

    Returns boolean

  • Returns the first item that satisfies the given predicate, or undefined if there is no such item.

    See also Array.find.

    Type Parameters

    • U

    Parameters

    • predicate: (value: T, index: number) => value is U

    Returns undefined | U

  • Returns the first item that satisfies the given predicate, or undefined if there is no such item.

    See also Array.find.

    Parameters

    • predicate: (value: T, index: number) => boolean

    Returns undefined | T

  • Returns the last item that satisfies the given predicate, or undefined if there is no such item.

    See also Array.findLast.

    Type Parameters

    • U

    Parameters

    • predicate: (value: T, index: number) => value is U

    Returns undefined | U

  • Returns the last item that satisfies the given predicate, or undefined if there is no such item.

    See also Array.findLast.

    Parameters

    • predicate: (value: T, index: number) => boolean

    Returns undefined | T

  • Returns a new array where every item has been replaced with the result of calling the given callback function. If the callback function returns an array, the items in that array are included individually.

    See also Array.flatMap.

    Type Parameters

    • U

    Parameters

    • callback: (value: T, index: number) => U | readonly U[]

    Returns ReactiveArray<U>

  • Removes the last item from this array and returns it, or undefined if the array was empty.

    See also Array.pop.

    Returns undefined | T

  • Calls the given callback function for all items in this array. The return value of the previous callback invocation is passed in the next call. The final result of the callback will be returned from this function.

    See also Array.reduce.

    Parameters

    • callback: (previousValue: T, currentValue: T, currentIndex: number) => T

    Returns T

  • Calls the given callback function for all items in this array. The return value of the previous callback invocation is passed in the next call. The final result of the callback will be returned from this function.

    See also Array.reduce.

    Parameters

    • callback: (previousValue: T, currentValue: T, currentIndex: number) => T
    • initialValue: T

    Returns T

  • Calls the given callback function for all items in this array. The return value of the previous callback invocation is passed in the next call. The final result of the callback will be returned from this function.

    See also Array.reduce.

    Type Parameters

    • U

    Parameters

    • callback: (previousValue: U, currentValue: T, currentIndex: number) => U
    • initialValue: U

    Returns U

  • Calls the given callback function for all items in this array, starting from the back. The return value of the previous callback invocation is passed in the next call. The final result of the callback will be returned from this function.

    See also Array.reduceRight.

    Parameters

    • callback: (previousValue: T, currentValue: T, currentIndex: number) => T

    Returns T

  • Calls the given callback function for all items in this array, starting from the back. The return value of the previous callback invocation is passed in the next call. The final result of the callback will be returned from this function.

    See also Array.reduceRight.

    Parameters

    • callback: (previousValue: T, currentValue: T, currentIndex: number) => T
    • initialValue: T

    Returns T

  • Calls the given callback function for all items in this array, starting from the back. The return value of the previous callback invocation is passed in the next call. The final result of the callback will be returned from this function.

    See also Array.reduceRight.

    Type Parameters

    • U

    Parameters

    • callback: (previousValue: U, currentValue: T, currentIndex: number) => U
    • initialValue: U

    Returns U

  • Removes the first value from this array and returns it, or undefined if the array was empty.

    See also Array.shift.

    Returns undefined | T

  • Returns true if at least one item satisfies the given predicate, false otherwise.

    See also Array.some.

    Parameters

    • predicate: (value: T, index: number) => boolean

    Returns boolean

  • Sorts this array using the given comparison function.

    See also Array.sort.

    Parameters

    • compare: (a: T, b: T) => number

    Returns void

  • Changes the contents of this array by removing, replacing and optionally adding new elements.

    See also Array.splice.

    Parameters

    • start: number
    • OptionaldeleteCount: number

    Returns T[]

  • Changes the contents of this array by removing, replacing and optionally adding new elements.

    See also Array.splice.

    Parameters

    • start: number
    • deleteCount: number
    • ...values: T[]

    Returns T[]

Properties

length: number

Returns the current number of items in this array.