You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would have expected that Typescript compiles this source without any complaints, as Javascript treats boolean values true and false as the numbers 1 and 0 in arithmetic expressions, but it seems that Typescript doesn't.
In fact the Javascript version of this function does work as expected:
functioncmp(a,b){return(a>b)-(a<b);}
cmp("foo", "bar") returns 1 and cmp("bar", "foo") returns -1.
Actual behavior:
Typescript complains with the following error messages:
src/boolarithmetic.ts:3:9 - error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
3 return (a > b) - (a < b);
~~~~~~~
src/boolarithmetic.ts:3:19 - error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
3 return (a > b) - (a < b);
~~~~~~~
The text was updated successfully, but these errors were encountered:
The whole purpose of TypeScript is to complain about code that looks fishy, such as relying on implicit conversions. It's easy to change this to an explicit conversion using return Number(a > b) - Number(a < b);.
TypeScript Version: 3.0.3
Search Terms: bool arithmetic
Code
Expected behavior:
I would have expected that Typescript compiles this source without any complaints, as Javascript treats boolean values
true
andfalse
as the numbers 1 and 0 in arithmetic expressions, but it seems that Typescript doesn't.In fact the Javascript version of this function does work as expected:
cmp("foo", "bar")
returns 1 andcmp("bar", "foo")
returns -1.Actual behavior:
Typescript complains with the following error messages:
The text was updated successfully, but these errors were encountered: