Readonly collections restrict mutation through a reference, not through every alias. They document consumer permissions without copying the data.
Before you start
You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.
The practical goal is to reason through this situation: A function accepting readonly number[] cannot push, but another owner may still mutate the original array. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Decide who can mutate
A consumer reading numbers should accept readonly input. This prevents accidental push or splice operations inside that consumer.
Step 2: Consider other aliases
The original owner may retain a mutable array reference. Readonly does not stop changes performed through that separate reference.
Step 3: Copy for independent ownership
If a snapshot must remain stable, create a copy. For nested objects, specify whether shallow copying is enough or nested values must also be copied.
Worked scenario
A function accepting readonly number[] cannot push, but another owner may still mutate the original array.
const owned = [1, 2];
const view: readonly number[] = owned;
const snapshot = [...view];
owned.push(3);
console.log(view.length); // 3
console.log(snapshot.length); // 2View and owned refer to the same array. Snapshot has its own array container; replacing or appending elements to owned does not change that container. Nested object elements would still be shared.
Common mistake
Readonly is neither deep immutability nor thread safety.
Verify the behavior
Attempt view.push in TypeScript, then mutate owned. Repeat with objects inside the array to demonstrate why a shallow snapshot does not guarantee deep immutability.
Interview exercise
Return a stable snapshot.
Answer and reasoning
Copy the data where independent ownership is needed and define how nested references are treated.
Continue learning
Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.