tezvyn:

TypeScript's Optional Chaining (`?.`)

Source: typescriptlang.orgintermediate

Optional chaining (`?.`) lets you safely access nested properties without crashing on `null` or `undefined`. Use it to replace verbose `&&` checks when traversing deep objects. The footgun is that it only checks the value to its left, not the entire chain.

Optional chaining (`?.`) provides a clean way to access nested properties, elements, or methods without crashing on `null` or `undefined` values, short-circuiting the expression to return `undefined` instead. It's ideal for safely traversing deep API responses or conditionally invoking optional callbacks, replacing verbose `&&` checks. The main footgun is that `?.` only checks the value to its immediate left; `foo?.bar.baz` will still crash if `foo.bar` is null.

Read the original → typescriptlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

TypeScript's Optional Chaining (`?.`) · Tezvyn