Quantcast
Channel: Node.js Tools for Visual Studio
Viewing all articles
Browse latest Browse all 4630

Edited Unassigned: TypeScript has issues with boolean arithmetic [1320]

$
0
0
Hi

The TypeScript error checker seems to trip up on boolean arithmetic when the booleans are implicitly coerced into being numbers: eg

```
if (true + true + false) ...
```

This construction is okay with the JavaScript error checker and it is often used to see whether more than one value is true: eg

```
var isAngry:boolean = true;
var isSad:boolean = true;
var isHappy:boolean = false;

if (isAngry + isSad + isHappy > 1)
console.log("You are confusing me!");
```

The IntelliSense error is

```
Invalid '+' expression - types not known to support the addition operator.
```

It is of course possible to get around this with explicit type conversion, but it would be nice to have implicit coercion working:

```
if (Number(isAngry) + Number(isSad) + Number(isHappy) > 1)
console.log("You are still confusing me!");
```
or

```
if (+isAngry + +isSad + +isHappy > 1)
console.log("You are confusing me!");
```
Many thanks

Todd

Viewing all articles
Browse latest Browse all 4630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>