no-unsafe-negation
JavaScript / TypeScriptsuspiciousno fixdefault: errorjs/eslint-recommendedjs/biome-recommendedjs/oxlint
What it does
Section titled “What it does”Reports !a in b and !a instanceof B, where the ! is written without parentheses on the left operand. !(a in b) and (!a) in b are not reported.
Why it matters
Section titled “Why it matters”The ! binds tighter than in and instanceof, so !a in b tests whether the key false (the negation of a) is in b, and !a instanceof B is always false for a boolean. The test the author meant, the negation of the whole expression, is never made.
To fix, write !(a in b) or !(a instanceof B).
Example
Section titled “Example”...if (!a in b) {} if (!a instanceof B) {} if ((!a) in b) {} if (!(a in b)) {}...Configuration
Section titled “Configuration”js: lint: rules: no-unsafe-negation: error # off, warn, error (default)Enabled by the presets: js/eslint-recommended, js/biome-recommended, js/oxlint.
Suppress one line with sudsy-disable-next-line no-unsafe-negation in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.
| Tool | Rule | Match | What differs |
|---|---|---|---|
| eslint | no-unsafe-negation |
partial | the enforceForOrderingRelations option is not supported |