TypeScript · Junior · Question 7 of 75

How do you declare an optional object property?

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.

Further reading

Problem with a question

TypeScript · Junior · question 7

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz