Skip to main content

No Optional Chaining

Ban the ?. operator so null/undefined checks are explicit because implicit chaining hides which values are expected to be nullable.

Summary

The no-optional-chaining rule reports any use of the optional chaining operator (?.), including optional property access (obj?.prop), optional element access (arr?.[0]), and optional calls (fn?.()).

The nullish coalescing operator (??) is not affected.

Why Use This Rule?

  1. Forces explicit null/undefined checks so the reader knows exactly which variable is nullable.
  2. Prevents deep chains of ?. that silently swallow missing intermediate objects.
  3. Keeps the nullish coalescing operator (??) available for providing defaults.

Examples

Configuration

Options

warning

ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.

OptionTypeDefaultDescription
ignoreFilesstring[][]File patterns to skip.

Autofix

Autofix is not available for this rule. Flagged code must be updated manually.

Troubleshooting

  • Ternary replacement feels verbose — Explicit checks like x !== undefined ? x.name : undefined are intentionally verbose. The verbosity signals that the value is nullable and forces the reader to consider both branches.
  • Nullish coalescing (??) is not flagged — Only the ?. operator is banned. ?? is a separate operator and is permitted.