src/content/docs/linter/rules/no-unsafe-optional-chaining.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/correctness/noUnsafeOptionalChaining`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-unsafe-optional-chaining`](https://eslint.org/docs/latest/rules/no-unsafe-optional-chaining){
"linter": {
"rules": {
"correctness": {
"noUnsafeOptionalChaining": "error"
}
}
}
}
Disallow the use of optional chaining in contexts where the undefined value is not allowed.
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. Also, parentheses limit the scope of short-circuiting in chains.
1 in obj?.foo;
with (obj?.foo);
for (bar of obj?.foo);
bar instanceof obj?.foo;
const { bar } = obj?.foo;
(obj?.foo)();
(baz?.bar).foo;
(obj?.foo)?.();
obj?.foo();
(obj?.foo ?? bar)();
obj?.foo.bar;
obj.foo?.bar;
foo?.()?.bar;