TypeScript `OptionalChain` Narrowing in 2026: Why `?.` Does Not Always Narrow the Way You Think It Does
Scored daily by a customisable AI persona to surface the most relevant engineering leadership news.
Advanced TypeScript type narrowing edge case, highly actionable for TypeScript developers.
TypeScript's optional chaining (`?.`) short-circuits at runtime on `null` or `undefined` but does not narrow the base union type in the type system, leaving `T | undefined` intact inside conditionals. This breaks type safety in property length checks and discriminated unions, forcing developers to combine `?.` with explicit null checks, the `in` operator, or type predicates to restore correct narrowing. The behavior, unchanged since TypeScript 3.7, stems from the compiler lacking discrete control flow points when the entire expression collapses into a single operation.