Readonly Arrays and Immutable Data
Use ReadonlyArray to prevent accidental mutations.
Readonly Arrays and Immutable Data 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.
Welcome
The readonly Modifier on Arrays
const ids: readonly number[] = [1, 2, 3];
// ids.push(4); // Error
// ids[0] = 99; // Error
console.log(ids[0]); // OK — reading is fineReadonlyArray<T> Generic Form
const names: ReadonlyArray<string> = ['Alice', 'Bob'];
// names.push('Charlie'); // ErrorReturning Readonly Arrays from Functions
function getConfig(): readonly string[] {
return ['host', 'port', 'db'];
}Immutable Function Parameters
function sum(nums: readonly number[]): number {
return nums.reduce((a, b) => a + b, 0);
// nums.push(99); // Error inside function
}as const for Literal Arrays
const colors = ['red', 'green', 'blue'] as const;
// type: readonly ['red', 'green', 'blue']
// colors.push('yellow'); // ErrorReadonly Does Not Deep-Freeze
const users: readonly { name: string }[] = [{ name: 'Alice' }];
// users.push({}); // Error
users[0].name = 'Bob'; // Allowed — object itself is mutableDeeply Immutable with Readonly<T>
type ImmutableUser = Readonly<{ name: string; scores: readonly number[] }>;
const u: ImmutableUser = { name: 'Alice', scores: [90, 85] };
// u.name = 'Bob'; // Error
// u.scores.push(100); // ErrorMutable vs Readonly Assignment
const ro: readonly number[] = [1, 2];
const mutable: number[] = ro; // Error!When to Use Readonly Arrays
Spreading Creates a New Mutable Array
const base: readonly number[] = [1, 2, 3];
const extended: number[] = [...base, 4]; // new mutable arrayQuick Check
Recap
Frequently asked questions
Is the “Readonly Arrays and Immutable Data” lesson free?
Yes — the full text of “Readonly Arrays and Immutable Data” 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 Immutable Data”?
Use ReadonlyArray to prevent accidental mutations. 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 Immutable Data” 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
- Typed Arrays: T[] and Array
- Readonly Arrays and Immutable Data
- Tuples: Fixed-Length Typed Arrays
- Enums: Numeric and String Constants