How to compare 2 boolean values in TypeScript

Viewed 4539

I'm trying to compare to boolean value in a if. I would like to do something like this:

value1 = false;
value2 = true;

if (value1 === value2) {
  ... Some code ...
}

In JAVA you can use Boolean.compare(boolean a, boolean b), but I can't find something equal in TypeScript.
For context, Boolean.compare(boolean a, boolean b) returns:

  • 0 if a is equal to b,
  • a negative value if a is false and b is true,
  • a positive value if a is true and b is false.

Thanks for you help

Edited: to show the message I get

enter image description here

This condition will always return 'false' since the types 'true' and 'false' have no overlap

1 Answers

Javascript doesn't have a builtin that's comparable to Java's Boolean.compare(). In fact, the Boolean class has nearly nothing in it, outside the constructor, toString() and valueOf().

If you want to replicate the functionality yourself, you can use the Number constructor.

function booleanCompare(a: boolean, b: boolean) {
    return Number(a) - Number(b);
}
Related