Skip to content
← Back to rules

typescript/no-confusing-non-null-assertion 可疑

An auto-fix is available for this rule.

它做了什么

禁止在可能引起混淆的位置使用非空断言。

为什么这是不好的?

在赋值或相等检查(= 或 == 或 ===)旁边使用非空断言(!)会导致代码令人困惑,因为其外观与“不等于”检查(!= !==)非常相似。

示例

此规则的错误代码示例:

ts
a! == b; // 非空断言(`!`) 和相等测试(`==`)
a !== b; // 不等于测试(`!==`)
a! === b; // 非空断言(`!`) 和三重相等测试(`===`)

此规则的正确代码示例:

ts
a == b;
a !== b;
a === b;

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "typescript/no-confusing-non-null-assertion": "error"
  }
}
bash
oxlint --deny typescript/no-confusing-non-null-assertion

参考资料