no-unsafe-negation
eslint
SuggestionRecommended
eslint.config.js
export default [
{
rules: {
"no-unsafe-negation": "error",
},
},
];Disallow negating the left operand of relational operators
if (!key in object) {
// operator precedence makes it equivalent to (!key) in object
// and type conversion makes it equivalent to (key ? "false" : "true") in object
}
if (!obj instanceof Ctor) {
// operator precedence makes it equivalent to (!obj) instanceof Ctor
// and it equivalent to always false since boolean values are not objects.
}if (!(foo) in object) {
// this is not an allowed exception
}if (!(key in object)) {
// key is not in object
}
if (!(obj instanceof Ctor)) {
// obj is not an instance of Ctor
}if ((!foo) in object) {
// allowed, because the negation is explicitly wrapped in parentheses
// it is equivalent to (foo ? "false" : "true") in object
// this is allowed as an exception for rare situations when that is the intended meaning
}
if(("" + !foo) in object) {
// you can also make the intention more explicit, with type conversion
}Examples from the ESLint documentation.
no-unsafe-negation
eslint
export default [
{
rules: {
"no-unsafe-negation": "error",
},
},
];no-unsafe-negation
eslint plugin · correctness
{
"rules": {
"no-unsafe-negation": "error"
}
}suspicious/noUnsafeNegation
suspicious · since 1.0.0
{
"linter": {
"rules": {
"suspicious": {
"noUnsafeNegation": "error"
}
}
}
}Disallow irregular whitespace
Disallow using an async function as a Promise executor
Disallow this/super before calling super() in constructors
Disallow comparing against -0
Disallow unnecessary boolean casts
Disallow characters which are made with multiple code points in character class syntax