Readonly properties in Typescript - Time & Space Complexity
Let's see how using readonly properties affects how long a program takes to run.
We want to know if readonly changes how many steps the program needs as input grows.
Analyze the time complexity of the following code snippet.
interface User {
readonly id: number;
name: string;
}
function updateUserName(user: User, newName: string) {
// user.id = 5; // Error: cannot assign to readonly property
user.name = newName;
}
This code defines a user with a readonly id and a name that can change. It updates the name but not the id.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated steps here.
- How many times: The update runs once per call, no matter the input size.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of steps stays the same no matter how many users or properties exist.
Time Complexity: O(1)
This means the time to update a property does not grow with input size; it stays constant.
[X] Wrong: "Readonly properties make the program slower because they add checks at runtime."
[OK] Correct: Readonly is a compile-time check in TypeScript and does not add extra steps when the program runs.
Understanding that readonly properties do not affect runtime speed shows you know the difference between compile-time and runtime, a useful skill in coding.
"What if we added a loop to update many user names at once? How would the time complexity change?"
Practice
readonly keyword do in TypeScript?Solution
Step 1: Understand the purpose of
Thereadonlyreadonlykeyword is used to mark properties that cannot be changed after initialization.Step 2: Compare options with the behavior
Only Prevents a property from being changed after it is set correctly describes that the property cannot be changed after it is set.Final Answer:
Prevents a property from being changed after it is set -> Option AQuick Check:
readonlymeans no changes allowed after set [OK]
- Thinking readonly allows changes anytime
- Confusing readonly with private
- Assuming readonly deletes properties
id in a TypeScript class?Solution
Step 1: Recall TypeScript syntax for readonly properties
The correct syntax is to placereadonlybefore the property name.Step 2: Check each option
readonly id: number; usesreadonly id: number;which is correct. const id: number; usesconstwhich is invalid inside class properties. private readonly id: number; addsprivatewhich is valid but not asked for. id readonly: number; has wrong order.Final Answer:
readonly id: number; -> Option BQuick Check:
Readonly keyword goes before property name [OK]
- Using const instead of readonly in class
- Placing readonly after property name
- Confusing access modifiers with readonly
class User {
readonly name: string;
constructor(name: string) {
this.name = name;
}
}
const user = new User('Alice');
user.name = 'Bob';
console.log(user.name);Solution
Step 1: Understand readonly property behavior
The propertynameis marked readonly, so it cannot be assigned a new value after construction.Step 2: Analyze the assignment
This line tries to change a readonly property, which causes a compile-time error in TypeScript.user.name = 'Bob'Final Answer:
Compile-time error -> Option DQuick Check:
Assigning to readonly property causes error [OK]
- Assuming readonly properties can be changed anytime
- Expecting runtime error instead of compile error
- Thinking output will be the new value
interface Point {
readonly x: number;
y: number;
}
const p: Point = { x: 10, y: 20 };
p.x = 15;
console.log(p.x);Solution
Step 1: Check readonly property assignment
The propertyxis readonly in the interface, so assigningp.x = 15is not allowed.Step 2: Identify the error message
TypeScript will report an error: "Cannot assign to 'x' because it is a readonly property".Final Answer:
Cannot assign to 'x' because it is a readonly property -> Option CQuick Check:
Readonly properties cannot be assigned after initialization [OK]
- Ignoring readonly and trying to assign anyway
- Thinking missing type annotation causes error
- Assuming no error occurs
Config where the version property is set once during construction and never changes, but settings can be updated anytime. Which is the best way to declare these properties?Solution
Step 1: Understand property requirements
Theversionshould be readonly (set once, no changes). Thesettingsshould be mutable (can change anytime).Step 2: Match options with requirements
readonly version: string; settings: object; correctly marksversionas readonly and leavessettingsmutable. version: string; readonly settings: object; reverses roles. readonly version: string; readonly settings: object; makes both readonly. version: string; settings: object; makes both mutable.Final Answer:
readonly version: string; settings: object; -> Option AQuick Check:
Readonly for fixed, mutable for changeable properties [OK]
- Making mutable properties readonly by mistake
- Not using readonly for fixed properties
- Confusing which property should be readonly
Ready to prove it?
Pass the test and this pattern moves unknown
