noExtraNonNullAssertion
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/suspicious/noExtraNonNullAssertion¥Diagnostic Category:
lint/suspicious/noExtraNonNullAssertion -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "suspicious": { "noExtraNonNullAssertion": "error" } } }}¥Description
防止在 TypeScript 文件中错误使用非空断言运算符 (!)。
¥Prevents the wrong usage of the non-null assertion operator (!) in TypeScript files.
TypeScript 中的
!非空断言运算符用于断言值的类型不包含null或undefined。在单个值上多次使用运算符不会产生任何效果。¥The
!non-null assertion operator in TypeScript is used to assert that a value’s type does not includenullorundefined. Using the operator any more than once on a single value does nothing.
¥Examples
¥Invalid
const bar = foo!!.bar;code-block.ts:1:13 lint/suspicious/noExtraNonNullAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Forbidden extra non-null assertion.
> 1 │ const bar = foo!!.bar;
│ ^^^^
2 │
ℹ Safe fix: Remove extra non-null assertion.
1 │ const·bar·=·foo!!.bar;
│ -
function fn(bar?: { n: number }) { return bar!?.n;}code-block.ts:2:10 lint/suspicious/noExtraNonNullAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Forbidden extra non-null assertion.
1 │ function fn(bar?: { n: number }) {
> 2 │ return bar!?.n;
│ ^^^^
3 │ }
4 │
ℹ Safe fix: Remove extra non-null assertion.
2 │ ··return·bar!?.n;
│ -
function fn(bar?: { n: number }) { return ((bar!))?.();}code-block.ts:2:12 lint/suspicious/noExtraNonNullAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Forbidden extra non-null assertion.
1 │ function fn(bar?: { n: number }) {
> 2 │ return ((bar!))?.();
│ ^^^^
3 │ }
4 │
ℹ Safe fix: Remove extra non-null assertion.
2 │ ··return·((bar!))?.();
│ -
¥Valid
const bar = foo!.bar;
obj?.string!.trim();
function fn(key: string | null) { const obj = {}; return obj?.[key!];}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号