readonly Arrays and Tuples
Lock arrays and tuples against mutation methods.
readonly Arrays and Tuples is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Readonly Arrays
A readonly array is an array you can read from but not modify. Write it as readonly number[] or equivalently ReadonlyArray<number>. The element type stays the same; only mutation is blocked.
const nums: readonly number[] = [1, 2, 3];
console.log(nums[0], nums.length);
// nums[0] = 9; // Error: index signature is readonlyThe ReadonlyArray Type
ReadonlyArray<T> is the generic form, identical in meaning to readonly T[]. Use whichever reads better; they're interchangeable.
const names: ReadonlyArray<string> = ['Ada', 'Sam'];
console.log(names.join(', '));No Mutating Methods
Readonly arrays omit mutating methods entirely. push, pop, splice, sort, and reverse are not available — the type literally doesn't have them.
const items: readonly string[] = ['a', 'b'];
// items.push('c'); // Error: push does not exist
// items.splice(0, 1); // Error
console.log(items.length);Non-Mutating Methods Still Work
Methods that return a new value rather than mutating — map, filter, concat, slice — are still available. They produce fresh arrays, leaving the original untouched.
const nums: readonly number[] = [1, 2, 3];
const doubled = nums.map((n) => n * 2);
console.log(doubled); // new array, nums unchangedReadonly Arrays as Parameters
Accepting readonly T[] in a function signals you won't mutate the caller's array. It documents intent and lets the compiler enforce the promise.
function sum(values: readonly number[]): number {
return values.reduce((a, b) => a + b, 0);
}
console.log(sum([10, 20, 30]));Mutable Arrays Are Assignable to Readonly
A regular number[] is assignable to a readonly number[] parameter — you're just promising not to mutate it. The reverse is not allowed.
function total(v: readonly number[]): number {
return v.reduce((a, b) => a + b, 0);
}
const mutable: number[] = [1, 2, 3];
console.log(total(mutable)); // okReadonly Tuples
Tuples can be readonly too: readonly [string, number]. The element types and order are fixed, and individual positions cannot be reassigned.
const pair: readonly [string, number] = ['age', 30];
console.log(pair[0], pair[1]);
// pair[1] = 31; // Error: readonly tupleReadonly Tuples Block Mutation
Like readonly arrays, readonly tuples remove mutating methods and reject index assignment. They're perfect for fixed-shape records such as coordinate pairs.
const point: readonly [number, number] = [3, 4];
// point.push(5); // Error
// point[0] = 0; // Error
console.log(point[0] + point[1]);const Assertion vs readonly Type
Two ways to get readonly arrays differ subtly. A readonly number[] annotation keeps the general element type. as const goes further: it makes a readonly tuple of literal types.
const a: readonly number[] = [1, 2, 3]; // elements: number
const b = [1, 2, 3] as const; // readonly [1, 2, 3]
console.log(a[0], b[0]);Choosing Between Them
Use a readonly T[] annotation when you want immutability but general element types. Use as const when you also need the exact literal values, for example to derive a union.
const DAYS = ['Mon', 'Tue', 'Wed'] as const;
type Day = typeof DAYS[number]; // 'Mon' | 'Tue' | 'Wed'
const d: Day = 'Tue';
console.log(d);Immutability for Shared Data
Readonly arrays and tuples protect shared constants and configuration lists from accidental mutation, making bugs from unexpected modification impossible at compile time.
const ALLOWED_PORTS: readonly number[] = [80, 443, 8080];
function isAllowed(p: number): boolean {
return ALLOWED_PORTS.includes(p);
}
console.log(isAllowed(443), isAllowed(22));Quick Check
Test your understanding of readonly arrays and tuples.
Recap: Readonly Arrays and Tuples
You learned that:
readonly T[]/ReadonlyArray<T>blocks mutation but allows reading and non-mutating methods.- Mutating methods like
push/spliceare removed from the type. - Readonly tuples fix both length and element types.
as constgoes further, producing readonly tuples of literal types.
Next, the readonly collection types.
const config: readonly string[] = ['a', 'b'];
const upper = config.map((s) => s.toUpperCase());
console.log(upper);Frequently asked questions
Is the “readonly Arrays and Tuples” lesson free?
Yes — the full text of “readonly Arrays and Tuples” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “readonly Arrays and Tuples”?
Lock arrays and tuples against mutation methods. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “readonly Arrays and Tuples” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- readonly Properties
- readonly Arrays and Tuples
- ReadonlyArray and ReadonlyMap
- Deep Immutability Patterns