Zoltán Tamási
Zoltán Tamási

Reputation: 12809

Comparison to FALSE is equivalent to negating value. Is it a ReSharper bug?

I'm using ReSharper 9, and its TypeScript features. I'm writing typescript code. I have an interface like this:

interface Test {
  A?: boolean;
  ...
}

And then I write an if statement like this, to test if member A exists and exactly equals to false. (aka.: if it does not exist, I want to treat it as true.)

if (myObj.A === false) {
  ...
}

Then ReSharper gives me a hint about Comparison to False is equivalent to negating value, and if I accept the fix, it becomes

if (!myObj.A) {
  ...
}

which is clearly not equivalent, because will be true if A is not defined for example.

What have I missed? Is it really equivalent at runtime?

Upvotes: 2

Views: 62

Answers (1)

Ross Scott
Ross Scott

Reputation: 1732

As you said the two comparisons aren't equivalent:

var myObj = {A: false};
myObj.A === false //true;
myObj = {};
myObj.A === false //false;
!myObj.A //true (not what you want)

So ReSharper is making an incorrect recommendation.

Upvotes: 2

Related Questions