useOptionalChain
Diagnostic Category: lint/complexity/useOptionalChain
Since: v1.0.0
Sources:
Enforce using concise optional chain instead of chained logical expressions.
TypeScript 3.7 introduced support for the optional chain operator, which was later standardized and included in the ECMAScript specification.
This operator allows you to safely access properties and methods on objects when they are potentially null
or undefined
.
The optional chain operator only chains when the property value is null
or undefined
.
It is much safer than relying upon logical operator chaining; which chains on any truthy value.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:1 lint/complexity/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Change to an optional chain.
> 1 │ foo && foo.bar && foo.bar.baz && foo.bar.baz.buzz
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Change to an optional chain.
1 │ - foo·&&·foo.bar·&&·foo.bar.baz·&&·foo.bar.baz.buzz
1 │ + foo?.bar?.baz?.buzz
2 2 │
code-block.js:1:1 lint/complexity/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Change to an optional chain.
> 1 │ foo.bar && foo.bar.baz.buzz
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Change to an optional chain.
1 │ - foo.bar·&&·foo.bar.baz.buzz
1 │ + foo.bar?.baz.buzz
2 2 │
code-block.js:1:1 lint/complexity/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Change to an optional chain.
> 1 │ foo !== undefined && foo.bar != undefined && foo.bar.baz !== null && foo.bar.baz.buzz
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Change to an optional chain.
1 │ - foo·!==·undefined·&&·foo.bar·!=·undefined·&&·foo.bar.baz·!==·null·&&·foo.bar.baz.buzz
1 │ + foo?.bar?.baz?.buzz
2 2 │
code-block.js:1:1 lint/complexity/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Change to an optional chain.
> 1 │ ((foo || {}).bar || {}).baz;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Change to an optional chain.
1 │ - ((foo·||·{}).bar·||·{}).baz;
1 │ + foo?.bar?.baz;
2 2 │
code-block.js:1:1 lint/complexity/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Change to an optional chain.
> 1 │ (await (foo1 || {}).foo2 || {}).foo3;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Change to an optional chain.
1 │ - (await·(foo1·||·{}).foo2·||·{}).foo3;
1 │ + (await·foo1?.foo2)?.foo3;
2 2 │
code-block.ts:1:1 lint/complexity/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Change to an optional chain.
> 1 │ (((typeof x) as string) || {}).bar;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Change to an optional chain.
1 │ - (((typeof·x)·as·string)·||·{}).bar;
1 │ + ((typeof·x)·as·string)?.bar;
2 2 │