Skip to content
← Back to rules

typescript/no-confusing-non-null-assertion Suspicious

🚧 An auto-fix is planned for this rule, but not implemented at this time.

它的作用

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

为什么这很糟糕?

在赋值或相等判断(=、== 或 ===)旁边使用非空断言(!)会使代码看起来像是不等判断(!= 或 !==),从而造成混淆。

示例

此规则的错误代码示例:

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

此规则的正确代码示例:

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

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/no-confusing-non-null-assertion": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/no-confusing-non-null-assertion": "error",
  },
});
bash
oxlint --deny typescript/no-confusing-non-null-assertion

版本

此规则在 v0.6.1 中加入。

参考资料