API reference

Array methods

37/37Coverage30/37Strict (zero caveats)

Every Array method is present, and arrays are full reference values: a length change (push/pop/splice) inside a callee is visible to the caller for every argument shape — a plain variable, an object-field or array-element array, or a HOF-callback element. The remaining recurring caveat is structural: the element-comparing helpers (indexOf/includes/join/sort) reject a nested-array element.

Exact Works, with caveats Not supported

[a, b, c] (literal)

Exact
const xs: T[] = [a, b, c]

Array literal. Element type is inferred from the first element.

const nums: number[] = [1, 2, 3, 4, 5]
See it in a full example →

new Array<T>(n)

Exact
new Array<T>(n: number): T[]

Constructs an array of length n. A length of 0 gives an empty array ready to push into.

let arr = new Array<number>(0)
arr.push(10)
See it in a full example →

.length

Works, with caveats
length: number

The number of elements. Read in the usual way.

⚠ Differences from standard
  • a.length = 2 (real JS's array-truncation idiom) hard compile-errors with "field assignment on non-object" — length is read-only in practice.

.push(...items)

Exact
push(...items: T[]): number

Appends items and returns the new length. Variadic (including the zero-argument call), and works on any mutable receiver — a variable, an object/class array field (this.items.push(x)), or a nested-array element (matrix[0].push(x)).

Returns

number — The new length.

let arr = new Array<number>(0)
arr.push(10)
let len: number = arr.push(20)   // 2
See it in a full example →

.pop()

Exact
pop(): T | undefined

Removes and returns the last element. Works on any mutable receiver (variable, object/class field, nested-array element).

Returns

T | undefined — The removed element, or `undefined` on an empty array.

let stack = [10, 20, 30]
let a = stack.pop()   // 30
See it in a full example →
⚠ Differences from standard
  • The absence result is a real T | undefined (TDD-00187): strict mode requires narrowing (!== undefined), a ?? default, or a ! assertion before use where a bare T is expected; -compat=js auto-widens.

.shift()

Exact
shift(): T | undefined

Removes and returns the first element, shifting the rest down. Works on any mutable receiver.

Returns

T | undefined — The removed element, or `undefined` on an empty array.

const done = list.titles.shift()
See it in a full example →
⚠ Differences from standard
  • The absence result is a real T | undefined (TDD-00187): strict mode requires narrowing (!== undefined), a ?? default, or a ! assertion before use where a bare T is expected; -compat=js auto-widens.

.unshift(...items)

Exact
unshift(...items: T[]): number

Prepends items and returns the new length. Variadic (including the zero-argument call), any mutable receiver.

Returns

number — The new length.

list.titles.unshift("wake up", "coffee in Thessaloniki")
See it in a full example →

.splice(start, delete?, ...items)

Exact
splice(start: number, deleteCount?: number, ...items: T[]): T[]

Removes deleteCount elements from start and inserts items in their place. deleteCount clamps to [0, len - start] and a negative start is normalized, matching real JS. Works on any mutable receiver.

Returns

T[] — The removed elements.

See it in a full example →

.slice(start?, end?)

Exact
slice(start?: number, end?: number): T[]

Returns a shallow copy of a section of the array (the whole array with no arguments). Negative indices count from the end. Does not mutate the original.

Returns

T[] — A new array.

.at(i)

Exact
at(i: number): T | undefined

Returns the element at index i, with negative indices counting from the end.

Returns

T | undefined — The element at i, or `undefined` when out of range (negative indices count from the end; one past the start is out of range, matching Node).

⚠ Differences from standard
  • The absence result is a real T | undefined (TDD-00187/TDD-00221, nested T[][] included): strict mode requires narrowing (!== undefined), a ?? default, or a ! assertion before use where a bare T is expected; -compat=js auto-widens.

.indexOf(item, fromIndex?)

Works, with caveats
indexOf(item: T, fromIndex?: number): number

Returns the index of the first matching element at or after the optional fromIndex (negative counts from the end), or -1.

Returns

number — First match index, or -1.

⚠ Differences from standard
  • Rejects a nested-array element (number[][]) — compares a bare register, no callback.

.includes(item)

Works, with caveats
includes(item: T): boolean

Whether the array contains the element.

Returns

boolean — true if present.

⚠ Differences from standard
  • Rejects a nested-array element (number[][]) — compares a bare register, no callback.

.find(fn)

Exact
find(fn: (x: T) => boolean): T | undefined

Returns the first element for which the predicate is true.

Returns

T | undefined — The first matching element, or `undefined` when nothing matches.

const first3 = [1,2,3,4,5].find(x => x > 3)   // 4
See it in a full example →
⚠ Differences from standard
  • The absence result is a real T | undefined (TDD-00187/TDD-00221, nested T[][] included): strict mode requires narrowing (!== undefined), a ?? default, or a ! assertion before use where a bare T is expected; -compat=js auto-widens.

.findIndex(fn)

Exact
findIndex(fn: (x: T) => boolean): number

Returns the index of the first element for which the predicate is true, or -1.

Returns

number — The index, or -1.

.some(fn)

Exact
some(fn: (x: T) => boolean): boolean

Whether the predicate is true for at least one element.

Returns

boolean — true if any element matches.

const hasEven = [1,2,3].some(x => x % 2 === 0)   // true
See it in a full example →

.every(fn)

Exact
every(fn: (x: T) => boolean): boolean

Whether the predicate is true for all elements.

Returns

boolean — true if every element matches.

const allPositive = [1,2,3].every(x => x > 0)   // true
See it in a full example →

.map(fn)

Exact
map(fn: (x: T) => U): U[]

Returns a new array with the callback applied to each element.

Returns

U[] — The mapped array.

const doubled = [1,2,3].map(x => x * 2)   // [2,4,6]
See it in a full example →

.filter(fn)

Exact
filter(fn: (x: T) => boolean): T[]

Returns a new array of the elements for which the predicate is true.

Returns

T[] — The filtered array.

const evens = [1,2,3,4].filter(x => x % 2 === 0)   // [2,4]
See it in a full example →

.reduce(fn, init?)

Exact
reduce(fn: (acc: U, x: T) => U, init?: U): U

Folds the array to a single value. The initial value is optional — without it the first element seeds the accumulator and the fold starts from the second. An empty array with no initial value throws, matching real JS.

Parameters
fn(acc, x) => U
Reducer called per element.
init?U
Seed accumulator; the first element is used if omitted.
Returns

U — The folded result.

const sum = [1,2,3,4,5].reduce((acc, n) => acc + n, 0)   // 15
See it in a full example →

.reduceRight(fn, init?)

Exact
reduceRight(fn: (acc: U, x: T) => U, init?: U): U

Folds the array to a single value right-to-left. Like .reduce() but visiting the last element first: without an initial value the last element seeds the accumulator and the fold walks leftward from the second-to-last; an empty array with no initial value throws, matching real JS.

Parameters
fn(acc, x) => U
Reducer called per element, from the end.
init?U
Seed accumulator; the last element is used if omitted.
Returns

U — The folded result.

const rtl = ['a','b','c','d'].reduceRight((acc, w) => acc + w, '')   // 'dcba'
See it in a full example →

.forEach(fn)

Exact
forEach(fn: (x: T, i?: number, arr?: T[]) => void): void

Runs the callback for each element, for its side effects. The callback receives the element and, if declared, the index and the source array (a live view over the same backing).

Returns

void — Nothing.

.join(sep?)

Exact
join(sep?: string): string

Joins the elements into a string, separated by sep (default ","). A nested-array element is rendered as its own comma-joined string (real JS's recursive toString), so [[1,2],[3,4]].join("-") is "1,2-3,4". Shares the array→string coercion path used by String(arr) and `${arr}`.

Returns

string — The joined string.

console.log(['hello','world'].join(', '))   // hello, world
See it in a full example →

.sort(fn?)

Exact
sort(fn?: (a: T, b: T) => number): T[]

Sorts the array in place, stably, with V8's TimSort — the same order as Node for any comparator. With no comparator the default is JS-faithful — elements are stringified and compared lexicographically, so `[10,1,21,2].sort()` is `[1,10,2,21]`; pass a comparator for a numeric sort. A custom comparator returns a negative, zero, or positive number; undefined elements go last.

Returns

T[] — The array, sorted in place.

const desc: number[] = [3, 1, 4, 1, 5]
desc.sort((a: number, b: number) => b - a)   // [5,4,3,1,1]
See it in a full example →

.reverse()

Exact
reverse(): T[]

Reverses the array in place.

Returns

T[] — The array, reversed in place.

.fill(val, start?, end?)

Exact
fill(val: T, start?: number, end?: number): T[]

Fills a section of the array with a static value, in place.

Returns

T[] — The array, filled in place.

.concat(...arrays)

Exact
concat(...arrays: T[][]): T[]

Returns a new array joining the receiver with the given arrays.

Returns

T[] — The concatenated array.

See it in a full example →

.flat(depth?)

Works, with caveats
flat(depth?: number): T[]

Flattens nested arrays. depth defaults to 1. Infinity flattens as deep as the receiver's own static type nests. Always returns a fresh array, even when nothing gets flattened.

Returns

T[] — A fresh flattened array.

See it in a full example →
⚠ Differences from standard
  • depth must be a compile-time constant integer or Infinity — this compiler's arrays have a fixed nesting depth at the type level, so the result's element type has to be known at compile time.

.flatMap(fn)

Exact
flatMap(fn: (x: T) => U | U[]): U[]

.map(fn) followed by exactly one level of flattening — like real JS, no depth parameter. A callback that doesn't return an array per element is just a plain map.

Returns

U[] — The mapped-and-flattened array.

See it in a full example →

.findLast(fn) / .findLastIndex(fn)

Exact
findLast(fn: (x: T) => boolean): T | undefined; findLastIndex(fn): number

Finds the last matching element / its index. Genuine reverse iteration, not a forward scan keeping the last match — the callback is invoked starting from the last element, matching real JS's reverse call order, observable via side effects.

See it in a full example →
⚠ Differences from standard
  • The absence result is a real T | undefined (TDD-00187/TDD-00221, nested T[][] included): strict mode requires narrowing (!== undefined), a ?? default, or a ! assertion before use where a bare T is expected; -compat=js auto-widens.

.toSorted() / .toReversed() / .toSpliced()

Exact
toSorted(fn?): T[]; toReversed(): T[]; toSpliced(start, deleteCount?, ...items): T[]

Non-mutating counterparts of .sort()/.reverse()/.splice() — sort or reverse a fresh copy, or build a fresh spliced result, leaving the original array untouched.

Returns

T[] — A fresh array; the receiver is unchanged.

See it in a full example →

.with(i, val)

Exact
with(i: number, val: T): T[]

Returns a fresh copy with the element at i replaced. Negative indices count from the end like .at(); an index still out of range after normalization throws a catchable Error, matching real JS's RangeError.

Returns

T[] — A fresh copy with one element replaced.

See it in a full example →

.keys() / .values() / .entries()

Exact
keys(): ArrayIterator<number>; values(): ArrayIterator<T>; entries(): ArrayIterator<[number, T]>

Return an Array Iterator over the array itself — next(), the iterator helpers (map, filter, take, drop, flatMap, reduce, toArray, forEach, some, every, find), for...of, spread and Array.from — which sees a later push, as in Node. entries() yields [index, value] tuples.

for (const [i, v] of arr.entries()) { /* ... */ }
See it in a full example →

.copyWithin(target, start?, end?)

Exact
copyWithin(target: number, start?: number, end?: number): T[]

Copies a section of the array to another position within it, in place. Overlap-safe via memmove — a self-overlapping copy such as arr.copyWithin(0, 3) on a 5-element array is handled correctly.

Returns

T[] — The array, modified in place.

See it in a full example →

Array.isArray(x)

Exact
Array.isArray(x: unknown): boolean

Whether the value is an array. Static method on Array.

Returns

boolean — true if x is an array.

Array.from(iterable, mapFn?)

Works, with caveats
Array.from(source, mapFn?: (v: T, i: number) => U): T[] | U[]

Builds an array from an array, a Set, a Map (as entries), a string (its characters), a generator, any iterable (a [Symbol.iterator]() method, a value held in `any`), a class implementing next(): T | null, or an array-like object (its length, then its indexed properties). An optional mapFn transforms each element by value and index. Static method on Array.

Returns

T[] | U[] — The constructed array (the mapped type when mapFn is given).

See it in a full example →
⚠ Differences from standard
  • thisArg (the third argument) is not supported.

Array.of(...items)

Exact
Array.of(...items: T[]): T[]

Builds an array from its arguments — the array literal [...] of them, usable anywhere one is; mixed argument kinds work as in a mixed literal. Static method on Array.

Returns

T[] — The constructed array.

See it in a full example →