Skip to main content
The ?. (optional chaining) operator evaluates an object property, array element, or function call, immediately short-circuiting and returning undefined if the left-hand side (LHS) operand evaluates to a nullish value (null or undefined). It strictly checks for nullish values, meaning it will continue evaluation for other falsy values such as 0, "", NaN, or false.

Type-Level Implications

At the type level, the TypeScript compiler automatically unions undefined to the evaluated expression’s return type. If a property prop is defined as type T, the optional chaining expression obj?.prop will resolve to type T | undefined. This type mutation ensures strict type safety by forcing subsequent operations to account for the potential undefined result yielded by a short-circuited chain.

Syntax Variants

The operator supports three distinct syntactic forms depending on the operation being performed: ?.identifier, ?.[expression], and ?.(args).

Evaluation Mechanics

When the TypeScript compiler processes the ?. operator, it injects a ternary-like nullish check.

Short-Circuiting Behavior

If the LHS operand is nullish, evaluation of the optional chain halts immediately. Short-circuiting applies strictly to the remainder of the optional chain itself, not to operations outside of it. Any subsequent property accesses, function calls, or side effects within the chain are bypassed, but adjacent expressions in the broader statement will still execute.
When chaining multiple ?. operators, the first nullish evaluation short-circuits the entire chain:

Syntactic Limitations

The optional chaining operator is strictly an accessor and evaluator. It triggers syntax errors when used in the following contexts:
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More