Skip to content

useOptionalChain

诊断类别:lint/complexity/useOptionalChain

¥Diagnostic Category: lint/complexity/useOptionalChain

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

强制使用简洁的可选链而不是链式逻辑表达式。

¥Enforce using concise optional chain instead of chained logical expressions.

TypeScript 3.7 引入了对可选链运算符的支持,该运算符后来被标准化并包含在 ECMAScript 规范中。当对象可能是 nullundefined 时,此运算符允许你安全地访问对象的属性和方法。可选链运算符仅在属性值为 nullundefined 时进行链接。它比依赖逻辑运算符链接要安全得多;链接到任何真值。

¥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

¥Invalid

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

¥Valid

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

¥Related links