I have to admit it made me laugh. That probably suggests it is far too cute.
Dunno why it is an advantage compared to an if/else chain; perhaps it is a consequence of narrowing by explicit comparison to booleans. (If so, why does it merit a mention in the release notes?!) Things like if (x == true) make me feel sorry for the person that wrote the code. I can see why typescript might want to accommodate it but it still makes me sad.
if (x == true) is dumb, but when I see it it’s usually the result of code changing over time, there was once a more complicated thing there and it’s been simplified by refactoring… But sometimes if (x === true) has a purpose. Although it’s the kind of red flag that should have a comment explaining why it needs to be there.
In my own code, I prefer using if (x == true), and by extension if (x == false), I don’t like truthy values, and I don’t like if (!x). I much prefer the slightly more verbose, as it reads nicer internally.
I like what you just did there. In my way too many years of coding I never once questioned the if (!x) way of writing. But you are right, if (x == false) indeed reads nicer internally.
I will never get to write if (x == false) though. cargo clippy will complain at me (yes yes, I can disable that, but I will not).
I was curious so I poked through the original issue which has some context. There’s an argument that the in case with lots of branches it could be more legible than many if/else’s. In terms of why this is now supported I suspect it’s more a case of “it’s valid JavaScript, so refining the type checker to support it might be valuable”.
That
switch(true)construction is an abomination, why would anybody do this?I have to admit it made me laugh. That probably suggests it is far too cute.
Dunno why it is an advantage compared to an if/else chain; perhaps it is a consequence of narrowing by explicit comparison to booleans. (If so, why does it merit a mention in the release notes?!) Things like if (x == true) make me feel sorry for the person that wrote the code. I can see why typescript might want to accommodate it but it still makes me sad.
if (x == true)is dumb, but when I see it it’s usually the result of code changing over time, there was once a more complicated thing there and it’s been simplified by refactoring… But sometimesif (x === true)has a purpose. Although it’s the kind of red flag that should have a comment explaining why it needs to be there.In my own code, I prefer using
if (x == true), and by extensionif (x == false), I don’t like truthy values, and I don’t likeif (!x). I much prefer the slightly more verbose, as it reads nicer internally.I like what you just did there. In my way too many years of coding I never once questioned the
if (!x)way of writing. But you are right,if (x == false)indeed reads nicer internally.I will never get to write
if (x == false)though.cargo clippywill complain at me (yes yes, I can disable that, but I will not).I can’t tell if you’re kidding or making a point, but for those not following along:
if (x == true)only checks for truthy values. (example)I was curious so I poked through the original issue which has some context. There’s an argument that the in case with lots of branches it could be more legible than many if/else’s. In terms of why this is now supported I suspect it’s more a case of “it’s valid JavaScript, so refining the type checker to support it might be valuable”.