eslint/no-unsafe-optional-chaining Correctness
What it does
Disallow use of optional chaining in contexts where the undefined value is not allowed
Why is this bad?
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:
Examples
Examples of incorrect code for this rule:
javascript
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; // TypeErrorConfiguration
This rule accepts a configuration object with the following properties:
disallowArithmeticOperators
type: boolean
default: false
Disallow arithmetic operations on optional chaining expressions. If this is true, this rule warns arithmetic operations on optional chaining expressions, which possibly result in NaN.
How to use
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-unsafe-optional-chaining": "error"
}
}bash
oxlint --deny no-unsafe-optional-chaining