When evaluating a boolean expression with =>
it is actually being evaluated to an arrow function, e.g. in the context of a conditional branch.
let x = -1
if (x => 0) {
console.log("true")
} else {
console.log("false")
}
This code will output true
, because the expression x => 0
becomes an anonymous function object, which is truthy.
An obvious solution would be to reverse the whole boolean expression by using <=
, which doesn’t suffer from the ambiguity with arrow function syntax. But that has the potential to reduce readability significantly, in the context of boolean algebra, which is already somewhat hard to reason about intuitively.
if (0 <= x) {
console.log("true");
} else {
console.log("false");
}
In TypeScript the problem is fundamentally the same, even though the no-implicity-any error can give a hint.
Is there an idiomatic way to force the boolean evaluation with greater-than-or-equal semantics?
1