Skip to content

unsafeNegations

Reports negating the left operand of in or instanceof relations.

✅ This rule is included in the ts untyped presets.

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 ((!
const key: any
key
) in
const object: any
object
) {
Error ts(2322) ― Type 'boolean' is not assignable to type 'string | number | symbol'.
}
if ((!
const object: any
object
) instanceof
const Constructor: any
Constructor
) {
Error ts(2358) ― The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
}
if ((!
const key: any
key
) in
const object: any
object
) {
Error ts(2322) ― Type 'boolean' is not assignable to type 'string | number | symbol'.
}

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 ❤️‍🔥 around the world by the Flint team and contributors.