Recall & Review
beginner
What does it mean when a property in a TypeScript interface is marked with a question mark (e.g.,
name?: string)?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 in a TypeScript interface?
Add a question mark
? after the property name, like age?: number.Click to reveal answer
intermediate
Can optional properties be used with any type in TypeScript?
Yes, optional properties can be of any type, including primitives, objects, arrays, or even other interfaces.
Click to reveal answer
beginner
What happens if you access an optional property that is not provided in an object?
The property value will be
undefined. You should check for undefined before using it.Click to reveal answer
beginner
Why are optional properties useful in TypeScript?
They allow flexibility in object shapes, letting you define properties that might not always be present without causing errors.
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, e.g.,
age?: number.What value does an optional property have if it is not provided in an object?
✗ Incorrect
If an optional property is missing, its value is
undefined.Which of these is a valid optional property declaration in TypeScript?
✗ Incorrect
The correct syntax uses a question mark after the property name:
name?: string.Can optional properties be used with complex types like arrays or objects?
✗ Incorrect
Optional properties can be of any type, including arrays, objects, or primitives.
Why might you use optional properties in an interface?
✗ Incorrect
Optional properties let you create flexible interfaces where some properties can be missing.
Explain how to declare and use optional properties in a TypeScript interface.
Think about how you make a property not required.
You got /4 concepts.
Describe a real-life scenario where optional properties in TypeScript interfaces are helpful.
Consider user data where some info is not always given.
You got /4 concepts.