Pular para o conteúdo

useOptionalChain

Este conteúdo não está disponível em sua língua ainda.

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.

foo && foo.bar && foo.bar.baz && foo.bar.baz.buzz
code-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

foo.bar && foo.bar.baz.buzz
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

foo !== undefined && foo.bar != undefined && foo.bar.baz !== null && foo.bar.baz.buzz
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

((foo || {}).bar || {}).baz;
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

(await (foo1 || {}).foo2 || {}).foo3;
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

(((typeof x) as string) || {}).bar;
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

foo && bar;
foo || {};
(foo = 2 || {}).bar;
foo || foo.bar;
foo["some long"] && foo["some long string"].baz