0
0
Typescriptprogramming~5 mins

Optional properties in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAdd an exclamation mark after the property name
BUse the keyword 'optional' before the property
CAdd a question mark after the property name
DUse the keyword 'nullable' before the property
What value does an optional property have if it is not provided in an object?
Anull
Bundefined
C0
Dempty string
Which of these is a valid optional property declaration in TypeScript?
Aname?: string
Bname!: string
Coptional name: string
Dname: string?
Can optional properties be used with complex types like arrays or objects?
AYes, any type can be optional
BNo, only primitive types can be optional
COnly arrays can be optional
DOnly objects can be optional
Why might you use optional properties in an interface?
ATo force all properties to be present
BTo make properties private
CTo make properties readonly
DTo allow some properties to be missing without errors
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.