3 ms·
> Often the appropriate fix is to switch from foo && someExpression to !!foo && someExpression. This seems... Wrong. Better leave a comment because other peopl
by black_puppydog 6y ago
> Often the appropriate fix is to switch from foo && someExpression to !!foo && someExpression.
This seems... Wrong. Better leave a comment because other people might assume it's a leftover from a refactoring or such.
- tpetry 6y agoI don‘t see a problem with it. !!foo and foo are in boolean logic the same, but as typescript does not allow (correctly) to use a string as a boolean value you‘re already doing !!foo for e.g. strings
- black_puppydog 6y agoYeah, I've only been working in TS for about a year, but I still find this sort of thing wrong. It's easy to get wrong since it overloads syntax (casting vs boolean ops) and is very hard to google (as many operators are).
- WorldMaker 6y agoYeah, I've started to migrate to the habit of using Boolean(expr) instead of !!expr simply because it is explicit about intent. I've seen projects with that as a tslint/eslint rule to never allow !! and instead it suggests Boolean() and I haven't quite done that in my own projects, but have considered it.