Recall & Review
beginner
What does the
readonly keyword do in TypeScript?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?✗ 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?
✗ 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?
✗ Incorrect
TypeScript gives a compile-time error to prevent changing readonly properties.
How do you declare a readonly property in an interface?
✗ Incorrect
The
readonly keyword is used before the property name in interfaces.Does
readonly make the contents of an array property immutable?✗ 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.