I have the following code:
var a: boolean = ...;
var b: boolean = ...;
if (a ^ b) { // this line gives error
// ...
}
However, the TypeScript compiler is giving an error:
error TS2113: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
Is it because bitwise operators only work for number? The code runs perfectly fine if written directly in JavaScript.
Is there any alternatives other than if (a ? !b : a) { ... }
?
Update
Given that both of them are boolean, I could just use a !== b
As you suggest, the ^
operator along with all the other bitwise operators can only be applied to the number type, according to the TypeScript spec.
The only workarounds I know of are:
<number><any>a ^ <number><any>b
a !== b
I opened an issue to track this on GitHub:
https://github.com/Microsoft/TypeScript/issues/587
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