Skip to content

unsafeNegations

Reports negating the left operand of in or instanceof 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.

if ((!key) in object) {
}
if ((!object) instanceof Constructor) {
}
if ((!key) in object) {
}

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.