This project is read-only.

Using bitwise operators/operations on boolean/bool values in TypeScript

Topics: Language Specification
May 29, 2013 at 6:08 PM
Is it intentional that TypeScript does not allow bitwise operations on bool/boolean values, while it works fine in other languages like JavaScript, Java and C#?

For example, I can do this in other languages, but not TypeScript:
var val1: bool = true;
var val2: bool = false;
val1 &= val2;
TypeScript has the following error if this is tried:
Operator '&=' cannot be applied to types 'bool' and 'bool'
I found this problem because I am writing a primitive utility which "transpiles" Java source code into TypeScript and this limitation of TypeScript would be quite involved to work around.
May 30, 2013 at 8:35 PM
This is disallowed because it's quite dangerous:
// Javascript
var x = true;
x &= true;
if(x === true) {
   console.log("You would think you'd be here");
} else {
   console.log("But you're actually here");