It makes a property unchangeable after it is initialized. You can set it once, but cannot modify it later.
Click to reveal answer
beginner
How do you declare a readonly property in a TypeScript interface?
Use the readonly keyword before the property name, like readonly name: string;.
Click to reveal answer
intermediate
Can you assign a value to a readonly property inside a class constructor?
Yes, you can assign a readonly property inside the constructor, but not modify it outside.
Click to reveal answer
beginner
What happens if you try to modify a readonly property after initialization?
TypeScript will give a compile-time error saying you cannot assign to a readonly property.
Click to reveal answer
intermediate
Can readonly properties be used with arrays or objects to make them immutable?
Readonly properties prevent reassigning the property itself, but do not make the contents of arrays or objects immutable.
Click to reveal answer
What does the readonly keyword do in TypeScript?
APrevents modification of a property after initialization
BMakes a property optional
CAllows a property to be changed anytime
DDeletes a property from an object
✗ Incorrect
The readonly keyword ensures a property cannot be changed after it is first set.
Where can you assign a value to a readonly property in a class?
AOnly inside the constructor
BAnywhere in the class
COnly outside the class
DNowhere, it must be undefined
✗ Incorrect
Readonly properties can be assigned or changed inside the constructor but not elsewhere.
What error occurs if you try to change a readonly property after initialization?
ASyntax error
BRuntime error
CCompile-time error
DNo error, it changes normally
✗ Incorrect
TypeScript gives a compile-time error to prevent changing readonly properties.
How do you declare a readonly property in an interface?
AUse <code>const</code> before the property name
BUse <code>readonly</code> before the property name
CUse <code>private</code> before the property name
DUse <code>static</code> before the property name
✗ Incorrect
The readonly keyword is used before the property name in interfaces.
Does readonly make the contents of an array property immutable?
AYes, but only for string arrays
BYes, it makes the array contents immutable
CNo, it deletes the array contents
DNo, it only prevents reassigning the property itself
✗ Incorrect
Readonly prevents changing the property reference, but array elements can still be changed.
Explain how readonly properties work in TypeScript and where you can assign their values.
Think about when and where you can set a readonly property.
You got /4 concepts.
Describe the difference between readonly properties and immutability of objects or arrays in TypeScript.
Consider what readonly protects and what it does not.
You got /4 concepts.
Practice
(1/5)
1. What does the readonly keyword do in TypeScript?
easy
A. Prevents a property from being changed after it is set
B. Allows a property to be changed anytime
C. Makes a property private and inaccessible
D. Deletes a property from an object
Solution
Step 1: Understand the purpose of readonly
The readonly keyword 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 A
Quick Check:
readonly means no changes allowed after set [OK]
Hint: Readonly means no changes allowed after initial set [OK]
Common Mistakes:
Thinking readonly allows changes anytime
Confusing readonly with private
Assuming readonly deletes properties
2. Which of the following is the correct way to declare a readonly property id in a TypeScript class?
easy
A. const id: number;
B. readonly id: number;
C. private readonly id: number;
D. id readonly: number;
Solution
Step 1: Recall TypeScript syntax for readonly properties
The correct syntax is to place readonly before the property name.
Step 2: Check each option
readonly id: number; uses readonly id: number; which is correct. const id: number; uses const which is invalid inside class properties. private readonly id: number; adds private which is valid but not asked for. id readonly: number; has wrong order.
Final Answer:
readonly id: number; -> Option B
Quick Check:
Readonly keyword goes before property name [OK]
Hint: Put readonly before property name in class [OK]
Common Mistakes:
Using const instead of readonly in class
Placing readonly after property name
Confusing access modifiers with readonly
3. What will be the output of this TypeScript code?
class User {
readonly name: string;
constructor(name: string) {
this.name = name;
}
}
const user = new User('Alice');
user.name = 'Bob';
console.log(user.name);
medium
A. Alice
B. Bob
C. undefined
D. Compile-time error
Solution
Step 1: Understand readonly property behavior
The property name is marked readonly, so it cannot be assigned a new value after construction.
Step 2: Analyze the assignment user.name = 'Bob'
This line tries to change a readonly property, which causes a compile-time error in TypeScript.
C. Cannot assign to 'x' because it is a readonly property
D. No error, code runs fine
Solution
Step 1: Check readonly property assignment
The property x is readonly in the interface, so assigning p.x = 15 is 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 C
Quick Check:
Readonly properties cannot be assigned after initialization [OK]
Hint: Readonly properties cannot be reassigned after creation [OK]
Common Mistakes:
Ignoring readonly and trying to assign anyway
Thinking missing type annotation causes error
Assuming no error occurs
5. You want to create a TypeScript class 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?
hard
A. readonly version: string; settings: object;
B. version: string; readonly settings: object;
C. readonly version: string; readonly settings: object;
D. version: string; settings: object;
Solution
Step 1: Understand property requirements
The version should be readonly (set once, no changes). The settings should be mutable (can change anytime).
Step 2: Match options with requirements
readonly version: string; settings: object; correctly marks version as readonly and leaves settings mutable. 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 A
Quick Check:
Readonly for fixed, mutable for changeable properties [OK]
Hint: Readonly for fixed, normal for changeable properties [OK]
Common Mistakes:
Making mutable properties readonly by mistake
Not using readonly for fixed properties
Confusing which property should be readonly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.