Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Arrays & Tuples · Lesson 9 of 12

Readonly Arrays & Tuples

A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
const locks the variable, but the array inside can still get poked and mutated. 🔐

Declaring an array with const stops you from reassigning the variable to a different array, but it does nothing to stop you from mutating the array itself — .push(), .pop(), and index assignment all still work. When you want to guarantee an array's contents can never change, TypeScript gives you the readonly modifier.

const colors: readonly string[] = ["red", "green", "blue"];

colors.push("yellow");
// Error: Property 'push' does not exist on type 'readonly string[]'.

colors[0] = "purple";
// Error: Index signature in type 'readonly string[]' only permits reading.
Adding readonly before string[] removes every mutating method — push, pop, splice, sort — from the type entirely, and blocks direct index assignment too.

readonly string[] is shorthand for a separate built-in type: ReadonlyArray<T>. The two are interchangeable, exactly like T[] and Array<T> from before — pick whichever reads better to you, though most codebases lean on the shorter readonly T[] form.

const ids: ReadonlyArray<number> = [1, 2, 3];

// identical meaning to:
const sameIds: readonly number[] = [1, 2, 3];
ReadonlyArray<number> and readonly number[] describe the same locked-down array type — neither allows mutating methods to be called on it.
🤖
Same lockdown, now applied to a fixed-position tuple. 🧱

Tuples can be locked down the same way. A readonly tuple still enforces its fixed length and per-position types, but on top of that, every position becomes unassignable after creation — you can read pair[0], but you can never write to it.

const pair: readonly [string, number] = ["Ada", 36];

pair[0] = "Grace";
// Error: Cannot assign to '0' because it is a read-only property.

console.log(pair[0]); // fine — reading is always allowed
The readonly modifier only blocks writes. Reading a value out of a readonly array or tuple works exactly the same as reading from a normal one.
Fill in the blank

Marking an array as ____ removes mutating methods like .push() and .sort() from its type entirely.

Quick check

Which type is equivalent to readonly number[]?

True or false

const scores: number[] = [1, 2, 3]; already prevents scores.push(4) from compiling.

Chapter quiz

Arrays & Tuples

10 questions · pass with 60% — just like in the app.

Quick check

How do you annotate a variable as an array of strings?

True or false

string[] and Array<string> describe the same type.

Fill in the blank

const values: number[] = [1, 2, "3"]; fails to compile because "3" is a ____, not a number.

Quick check

What makes a tuple different from a regular typed array like number[]?

True or false

let pair: [string, number] = [1, "a"]; compiles without error.

Fill in the blank

For let entry: [string, number] = ["hp", 50];, entry[0] has the type ____.

Quick check

What does adding readonly before an array type do?

True or false

ReadonlyArray<number> and readonly number[] describe the same type.

Fill in the blank

const list: readonly string[] = ["a"]; list.push("b"); fails because push does not exist on a ____ array.

Quick check

On a readonly tuple, which operation is still allowed?

Keep your streak going in Swipe Coding

Swipe through every level of TypeScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →