Readonly properties help you protect data from being changed by mistake. They make sure some values stay the same after you set them.
Readonly properties in interfaces in Typescript
interface InterfaceName {
readonly propertyName: type;
}The readonly keyword goes before the property name.
Once set, you cannot assign a new value to a readonly property.
id cannot be changed after it is set, but name can be updated.interface User {
readonly id: number;
name: string;
}x and y are readonly, so the point's position cannot be changed.interface Point {
readonly x: number;
readonly y: number;
}This program shows a car object with a readonly vin. You can change the model but not the vin.
interface Car {
readonly vin: string;
model: string;
}
const myCar: Car = { vin: "123ABC", model: "Sedan" };
console.log(myCar.vin); // prints the VIN
myCar.model = "Coupe"; // allowed
// myCar.vin = "456DEF"; // Error: Cannot assign to 'vin' because it is a read-only property
console.log(myCar.model);Readonly only stops changes after the object is created; you must set the value when you create the object.
Readonly works well with interfaces to describe data shapes that should not change.
Use readonly in interfaces to protect properties from being changed.
This helps avoid bugs by making some data fixed after creation.
Practice
What does the readonly keyword do when used in a TypeScript interface property?
Solution
Step 1: Understand the purpose of
Thereadonlyin interfacesreadonlykeyword marks a property so it cannot be reassigned after the object is created.Step 2: Compare with other options
Options A, B, and D describe different behaviors: A makes optional, B allows changes, D changes visibility, none of which matchreadonly.Final Answer:
Prevents the property from being changed after initialization -> Option AQuick Check:
readonlymeans no changes allowed after creation [OK]
- Thinking readonly makes property optional
- Confusing readonly with private or protected
- Assuming readonly allows reassignment
Which of the following is the correct syntax to declare a readonly property id of type number in a TypeScript interface?
interface User {
?
}Solution
Step 1: Recall correct readonly syntax in interfaces
The correct syntax placesreadonlybefore the property name, then a colon and the type.Step 2: Check each option
readonly id: number; matches the correct syntax. Options A, B, and C have incorrect order or misplaced keywords.Final Answer:
readonly id: number; -> Option DQuick Check:
Readonly keyword comes before property name [OK]
- Placing readonly after property name
- Using readonly as a type
- Misplacing colons or semicolons
Consider the following code:
interface Point {
readonly x: number;
readonly y: number;
}
const p: Point = { x: 10, y: 20 };
p.x = 30;
console.log(p.x);What will happen when this code runs?
Solution
Step 1: Understand readonly behavior at compile time
TypeScript prevents assigning a new value to a readonly property during compilation, causing an error before running.Step 2: Check runtime behavior
Since TypeScript errors stop compilation, the code won't run, so no runtime error or output occurs.Final Answer:
Compile-time error due to assignment to readonly property -> Option CQuick Check:
Readonly blocks assignment at compile time [OK]
- Thinking readonly allows reassignment at runtime
- Expecting runtime error instead of compile error
- Assuming original value changes silently
Find the error in this code snippet:
interface Config {
readonly url: string;
}
const config: Config = { url: "https://api.com" };
config.url = "https://newapi.com";Solution
Step 1: Identify the readonly property usage
The propertyurlis marked readonly, so it cannot be reassigned after initialization.Step 2: Check the assignment line
The lineconfig.url = "https://newapi.com";tries to change a readonly property, causing a compile-time error.Final Answer:
Cannot assign to 'url' because it is a readonly property -> Option AQuick Check:
Readonly properties cannot be reassigned [OK]
- Ignoring readonly and trying to reassign
- Confusing readonly with optional properties
- Thinking semicolon or type is the error
You want to create an interface Person with a readonly id and a mutable name. Which code correctly enforces this?
?
Solution
Step 1: Define readonly and mutable properties correctly
Thereadonlykeyword must be before the property name to make it immutable; properties without it remain mutable.Step 2: Evaluate each option
interface Person { readonly id: number; name: string; } correctly marksidas readonly andnameas mutable. Options B, C, and D misuse readonly or make both properties readonly.Final Answer:
interface Person { readonly id: number; name: string; } -> Option BQuick Check:
Readonly before property name only for immutable fields [OK]
- Placing readonly after property name or type
- Making all properties readonly by mistake
- Confusing readonly with type annotations
Ready to prove it?
Pass the test and this pattern moves unknown
