unsafeNegations
Reports negating the left operand of
in
orinstanceof
relations.
✅ This rule is included in the ts untyped
preset.
Negating the left-hand side of in
or instanceof
changes the meaning due to operator precedence.
Use !(left in right)
or !(left instanceof Right)
to negate the relation as intended.
Examples
Section titled “Examples”if ((!key) in object) {}
if ((!object) instanceof Constructor) {}
if ((!key) in object) {}
if (!(key in object)) {}
if (!(object instanceof Constructor)) {}
if (key in object) {}if (object instanceof Constructor) {}
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase never negates in
or instanceof
relations, this rule may provide limited value.
However, keeping it enabled prevents subtle precedence mistakes that change control-flow conditions.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noUnsafeNegation
- Deno:
no-unsafe-negation
- ESLint:
no-unsafe-negation
- Oxlint:
eslint/no-unsafe-negation
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.