Question presented to candidate: "An API returns users where `email` and `address` are sometimes missing. How do you type those fields, and how do you read `user.address.city` without crashing? Walk me through optional properties and optional chaining."
What a strong answer should cover:
- `key?: T` means the key may be absent; reading it gives `T | undefined` and must be narrowed under `strict`
- `key?: T` is not the same as `key: T | undefined` (the second still requires the key); `in` tells "missing" from "present but undefined"; `exactOptionalPropertyTypes` makes the distinction strict
- Optional chaining has three forms: `a?.b`, `a?.[k]` and `f?.()`, and the result type always adds `undefined`
- Short-circuiting: once the left side is `null` or `undefined`, the rest of the chain, including function calls and index expressions, is not evaluated
- `?.` guards only `null`/`undefined` (not `0` or `""`), only the link it is written on, and cannot be used on the left of `=`; pair it with `??` for defaults
- Optional methods and `Partial<T>`
Code / implementation expected: Yes β an interface with optional fields and safe reads with `?.` and `??`.