Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

oxc/no-optional-chaining

Disallow optional chaining (?.)

Engines older than ES2020 can't parse ?., so a file that uses it fails to load there. If a compiler lowers it for a target like that, each ?. turns into temporary variables and null and undefined checks, which makes the output larger.

The rule reports each chain once, no matter how many ?. it has.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"oxc/no-optional-chaining" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"oxc/no-optional-chaining" = ["error", {
  message = "", # string
}]

Examples

Reported:

const city = user?.address?.city;
const first = items?.[0];
onSave?.(first);

Not reported:

const city = user && user.address && user.address.city;

if (onSave) {
  onSave(city);
}

Options

OptionDefaultWhat it does
message""Text to show as help under the report, for example why your project doesn't allow ?..
const city = user?.address;

When not to use it

Every current browser and runtime supports ?.. Only use this rule if your code has to run on older ones.