useOptionalChain
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/complexity/useOptionalChain¥Diagnostic Category:
lint/complexity/useOptionalChain -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "complexity": { "useOptionalChain": "error" } } }}¥Description
强制使用简洁的可选链而不是链式逻辑表达式。
¥Enforce using concise optional chain instead of chained logical expressions.
TypeScript 3.7 引入了对可选链运算符的支持,该运算符后来被标准化并包含在 ECMAScript 规范中。当对象可能是 null 或 undefined 时,此运算符允许你安全地访问对象的属性和方法。可选链运算符仅在属性值为 null 或 undefined 时进行链接。它比依赖逻辑运算符链接要安全得多;链接到任何真值。
¥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.buzzcode-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.buzzcode-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.buzzcode-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
Biome v2.1 中文网 - 粤ICP备13048890号