Skip to content

no-unsafe-negation

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.

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).

Reported
...
if (!a in b) {} if (!a instanceof B) {} if ((!a) in b) {} if (!(a in b)) {}
...
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.

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