Allow booleans to be treated as numbers.
One thing I often did in JavaScript was use booleans as numbers, as it was often easier to do that then add a whole bunch of extra ternary operators / if statements. Currently though when I move my JavaScript into TypeScript it gets mad at me for doing things like (bool * number) or (bool - bool) which means that I have to go around wrapping all my booleans in Number(), which I would prefer not to do.
Seeing as var x = 'five' + 5; is considered perfectly fine it seems strange that adding booleans and numbers raises errors.
My comment is subjective and based on my personal opinion:
I think string + number is already borderline. bool * number makes no sense. I know it "works" in JS, but it's too arcane.
Following this logic, you can really remove all type checks from addition, as you could argue JS allows you to {} + [].
Moreover, it means that anyone who writes bool * number by mistake now doesn't get an error, although for many coder it probably is an error.
Regarding your code, I personnally would prefer bool ? number : 0 to bool * number as its more explicit as to what your intents are (and only 2 chars longer). I'm not sure about bool - bool as I don't really get what you're trying to do (not a good sign). false - true is -1, what do you do with that?
My use case for pretty much all this is converting user input (several bools representing which keys are held down) into moving a character. So the bool - bool thing is rightKey - leftKey in order to get the desired change in x.
Although the rest of what you are saying makes sense, and to be honest this is probably one of the only situations in which I will need it.
-1
+rightKey - +leftKey will work and is only two characters longer.
@jeffreymorlan Thank you!
Yes in js unary plus implicitly converts to a number. Personnally I think it's an obscure way to code your movement logic. I'd much rather do:
if (rightKey) dx++;
if (leftKey) dx--;
Yes it's two lines of code, but it's very clear even to a js newcomer. In my book clarity wins over terseness.
@jods4 That is a very valid point, and some searching has told me that most people agree with you in regards to clarity vs terseness. I just have some sort of addiction to one liners and avoiding temporary variables, although I am getting better.
Yeah, unary plus is the pattern you want here that is still terse and expressive but also clearly declares your intent. The majority of TypeScript users will be better off if accidental uses of bools and numbers are errors and you can use this pattern to work in a type safe fashion.
How are:
true + 'foo'5 + 'foo'
valid and not subject to accidental use while 4 + true is invalid?
If the language allows it why would typescript prevent its use ?
It's a gray area. The language allows {} / [] but that doesn't mean it'd be wrong for TypeScript to consider that an error.
Any concatenation with a string is allowed because expressions like count + ' items are in your cart' are incredibly common and generally not error-prone.