0Pricing
TypeScript Academy · Lesson

Readonly Arrays and Immutable Data

Use ReadonlyArray to prevent accidental mutations.

Welcome

Mutable arrays can be accidentally modified anywhere in your code. TypeScript's ReadonlyArray and readonly modifier help you enforce immutability.

The readonly Modifier on Arrays

Add the `readonly` modifier before the array type to prevent mutation. The array can be read but not changed.
const ids: readonly number[] = [1, 2, 3];
// ids.push(4);   // Error
// ids[0] = 99;   // Error
console.log(ids[0]); // OK — reading is fine

All lessons in this course

  1. Typed Arrays: T[] and Array
  2. Readonly Arrays and Immutable Data
  3. Tuples: Fixed-Length Typed Arrays
  4. Enums: Numeric and String Constants
← Back to TypeScript Academy