Recall & Review
beginner
What does it mean when a property in a TypeScript interface is marked with a question mark (e.g.,
age?: number)?It means the property is optional. Objects implementing the interface may or may not include that property.
Click to reveal answer
beginner
How do you declare an optional property named
email of type string in a TypeScript interface?You write
email?: string; inside the interface definition.Click to reveal answer
beginner
Can an object that implements an interface with optional properties omit those properties?
Yes, optional properties can be left out when creating an object that implements the interface.
Click to reveal answer
intermediate
What happens if you try to access an optional property that is not provided in an object?
The property will be
undefined. You should check if it exists before using it.Click to reveal answer
intermediate
Why are optional properties useful in TypeScript interfaces?
They allow flexibility by letting objects have some properties but not requiring all, making interfaces adaptable to different situations.
Click to reveal answer
How do you mark a property as optional in a TypeScript interface?
✗ Incorrect
In TypeScript, a question mark (?) after the property name marks it as optional.
If an interface has an optional property, can an object omit that property when implementing the interface?
✗ Incorrect
Optional properties can be left out when creating objects that implement the interface.
What is the type of an optional property if it is not provided in an object?
✗ Incorrect
If an optional property is missing, its value is undefined.
Which of the following is a correct optional property declaration in an interface?
✗ Incorrect
The correct syntax uses a question mark after the property name.
Why might you use optional properties in an interface?
✗ Incorrect
Optional properties let objects include some properties but not require all, adding flexibility.
Explain what optional properties in TypeScript interfaces are and how they affect object creation.
Think about how you can leave some details out when describing something.
You got /4 concepts.
Describe a real-life example where optional properties in an interface would be useful.
Consider a form where some fields are not required.
You got /3 concepts.