Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are logical assignment (&=) operators disallowed in Typescript?

Tags:

typescript

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?

like image 332
cloakedninjas Avatar asked Mar 06 '16 13:03

cloakedninjas


1 Answers

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.

like image 112
Martin Liversage Avatar answered Oct 17 '22 07:10

Martin Liversage