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
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.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];
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
Marking an array as ____ removes mutating methods like .push() and .sort() from its type entirely.
Which type is equivalent to readonly number[]?
const scores: number[] = [1, 2, 3]; already prevents scores.push(4) from compiling.
Arrays & Tuples
10 questions · pass with 60% — just like in the app.
How do you annotate a variable as an array of strings?
string[] and Array<string> describe the same type.
const values: number[] = [1, 2, "3"]; fails to compile because "3" is a ____, not a number.
What makes a tuple different from a regular typed array like number[]?
let pair: [string, number] = [1, "a"]; compiles without error.
For let entry: [string, number] = ["hp", 50];, entry[0] has the type ____.
What does adding readonly before an array type do?
ReadonlyArray<number> and readonly number[] describe the same type.
const list: readonly string[] = ["a"]; list.push("b"); fails because push does not exist on a ____ array.
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 →