readonly
readonly marks a property or an array as read-only: TypeScript will not let you change it after it is created.
readonly is a type modifier. It tells the type checker not to allow writing to something.
With readonly string[], the array has no push, and you cannot assign with items[0] = .... You can only read it. On a property, like readonly id: number, you cannot reassign it.
The course uses it like this: export const ALLOWED_ORIGINS: readonly string[] = [...]. const stops you from reassigning the variable, but not from changing the array's contents. readonly closes that gap so nobody changes the list by mistake.
Note: readonly only exists in type checking. When Node runs the code, the type is gone, and the array is still a normal array. It is a compile-time protection, not a runtime one.
Reach for it when: you have data that should not change after it is created, like configuration lists or an object's id.
Examples
Common mistake
const origins: readonly string[] = ["a"];
origins.push("b");Why: A `readonly string[]` has no `push` method, because `push` would change the array.
How to write it: Create a new array: const more = [...origins, "b"];.
Common mistake
interface User {
readonly id: number;
}
const user: User = { id: 1 };
user.id = 2;Why: `id` is readonly, so TypeScript does not allow assigning a new value to it.
How to write it: Create a new object with the different id, or remove readonly if it really must change.
