With the following code:
var x:boolean = true;
x &= false;
Results in error TS2447: The '&=' operator is not allowed for boolean types. Consider using '&&' instead.
I've looked around but can't find a reason why, there's a PR to make the error what it is: https://github.com/Microsoft/TypeScript/issues/712 but still can't find the underlying reason for it.
Can someone clarify?
I can't speak on behalf of the designers of TypeScript but the & (bitwise AND) operator is intended to perform the a bitwise AND operation on two integers. Your variable is a boolean and these values are combined using && (logical AND).
In TypeScript you could conceivably create an &&=
operator but the &&
operator uses short-circuit evaluation where evaluation stops as soon the result is known which means that the semantic of x &&= y
becomes a bit clouded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With