no-unsafe-optional-chaining
Disallow use of optional chaining in contexts where the undefined
value is not allowed
Using the recommended
config from @eslint/js
in a configuration file enables this rule
The optional chaining (?.
) expression can short-circuit with a return value of undefined
. Therefore, treating an evaluated optional chaining expression as a function, object, number, etc., can cause TypeError or unexpected results. For example:
var obj = undefined;
1 in obj?.foo; // TypeError
with (obj?.foo); // TypeError
for (bar of obj?.foo); // TypeError
bar instanceof obj?.foo; // TypeError
const { bar } = obj?.foo; // TypeError
Also, parentheses limit the scope of short-circuiting in chains. For example:
var obj = undefined;
(obj?.foo)(); // TypeError
(obj?.foo).bar; // TypeError
Rule Details
This rule aims to detect some cases where the use of optional chaining doesn’t prevent runtime errors. In particular, it flags optional chaining expressions in positions where short-circuiting to undefined
causes throwing a TypeError afterward.
Examples of incorrect code for this rule:
/*eslint no-unsafe-optional-chaining: "error"*/
()();
().bar;
().bar;
().bar();
(obj?.foo ?? )();
(foo || )();
( && foo)();
(foo ? : bar)();
(foo, ).baz;
()`template`;
new ()();
[...];
bar(...);
1 in ;
bar instanceof ;
for (bar of );
const { bar } = ;
[{ bar } = ] = [];
with ();
class A extends {}
var a = class A extends {};
async function foo () {
const { bar } = await ;
(await )();
(await ).bar;
}
Examples of correct code for this rule:
/*eslint no-unsafe-optional-chaining: "error"*/
(obj?.foo)?.();
obj?.foo();
(obj?.foo ?? bar)();
obj?.foo.bar;
obj.foo?.bar;
foo?.()?.bar;
(obj?.foo ?? bar)`template`;
new (obj?.foo ?? bar)();
var baz = {...obj?.foo};
const { bar } = obj?.foo || baz;
async function foo () {
const { bar } = await obj?.foo || baz;
(await obj?.foo)?.();
(await obj?.foo)?.bar;
}
Options
This rule has an object option:
disallowArithmeticOperators
: Disallow arithmetic operations on optional chaining expressions (Defaultfalse
). If this istrue
, this rule warns arithmetic operations on optional chaining expressions, which possibly result inNaN
.
disallowArithmeticOperators
With this option set to true
the rule is enforced for:
- Unary operators:
-
,+
- Arithmetic operators:
+
,-
,/
,*
,%
,**
- Assignment operators:
+=
,-=
,/=
,*=
,%=
,**=
Examples of additional incorrect code for this rule with the { "disallowArithmeticOperators": true }
option:
/*eslint no-unsafe-optional-chaining: ["error", { "disallowArithmeticOperators": true }]*/
+;
-;
+ bar;
- bar;
/ bar;
* bar;
% bar;
** bar;
baz += ;
baz -= ;
baz /= ;
baz *= ;
baz %= ;
baz **= ;
async function foo () {
+await ;
await + bar;
baz += await ;
}
Version
This rule was introduced in ESLint v7.15.0.