TypeScript · Junior · Question 7 of 75
How do you declare an optional object property?
- ✓Correct answer:
{ name?: string } - 2
{ name: optional string } - 3
{ ?name: string }
Explanation
A ? after the name makes the property optional: its type is string | undefined, and the key itself may be missing. With the exactOptionalPropertyTypes flag, explicitly assigning undefined is distinguished from a missing key.