TypeScript
Optional chaining
?. safely accesses properties or calls methods when a value may be null or undefined.
Example
const user = { profile: { email: "test@mail.com" } };
user.profile?.email;
user.account?.email;Common forms
- Method call:
user.getInfo?.() - Array access:
cart?.[0] - With
??:user.profile?.email ?? "No email"
Notes
- Only guards
nullandundefined. - Supported since TypeScript 3.7.