Recall & Review
beginner
What does the
readonly keyword do in a TypeScript interface?It makes a property unchangeable after the object is created. You can read the value but cannot modify it.
Click to reveal answer
beginner
How do you declare a readonly property
name of type string in an interface?Use
readonly name: string; inside the interface definition.Click to reveal answer
beginner
Can you assign a new value to a readonly property after the object is created?
No, TypeScript will give an error if you try to change a readonly property after initialization.
Click to reveal answer
intermediate
Why use readonly properties in interfaces? Give a real-life example.
To protect important data from accidental changes. For example, a user ID should not change once set.
Click to reveal answer
beginner
Can readonly properties be set during object creation?
Yes, readonly properties can be set when the object is first created but not changed later.
Click to reveal answer
What happens if you try to change a readonly property in TypeScript?
✗ Incorrect
Readonly properties cannot be changed after initialization, so TypeScript shows a compile-time error.
How do you mark a property as readonly in a TypeScript interface?
✗ Incorrect
The
readonly keyword is used before the property name to make it unchangeable.Can a readonly property be assigned a value when creating an object?
✗ Incorrect
Readonly properties can be assigned a value when the object is created but cannot be changed afterward.
Which of these is a good reason to use readonly properties?
✗ Incorrect
Readonly properties help protect important data from being changed by mistake.
If an interface has a readonly property, can you omit it when creating an object?
✗ Incorrect
Readonly does not mean required; if the property is optional, you can omit it.
Explain what readonly properties in interfaces are and why they are useful.
Think about how you keep some information fixed, like a birthdate.
You got /4 concepts.
Describe how to declare and use a readonly property in a TypeScript interface with a simple example.
Show how to write the interface and create an object with that property.
You got /3 concepts.