ReadonlyArray<T>

Represents an array of T whose elements cannot be modified.

Since TS 1.6 Spec ↗

Syntax

ReadonlyArray<Type>

Returns

array type — An immutable array view exposing only non-mutating members.

Examples

const nums: ReadonlyArray<number> = [1, 2, 3];
// nums.push(4); // Error: push does not exist
const doubled = nums.map(n => n * 2);
function sum(values: readonly number[]): number {
  return values.reduce((a, b) => a + b, 0);
}

Notes

Equivalent to the shorthand `readonly T[]`. Mutating methods like `push`, `pop`, and index assignment are removed at the type level. A regular array is assignable to a `ReadonlyArray`, but not the reverse.

See also