typescript/no-confusing-non-null-assertion Suspicious
它的作用
禁止在可能引起混淆的位置使用非空断言。
为什么这很糟糕?
在赋值或相等性检查(=、== 或 ===)旁边使用非空断言(!),会产生令人困惑的代码,因为它看起来类似于不等性检查(!= 或 !==)。在 in 或 instanceof 检查旁边使用非空断言也会令人困惑,因为它看起来可能像是对运算符进行了否定。
示例
此规则的错误代码示例:
ts
a! == b; // 非空断言(`!`)和相等性测试(`==`)
a !== b; // 不等性测试(`!==`)
a! === b; // 非空断言(`!`)和全等性测试(`===`)
a! in b;
a! instanceof b;此规则的正确代码示例:
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 中加入。
