Re:JavaScript for squares: The incredible rise of TypeScript
by tonyedgecombe on Wednesday July 26, 2017 @03:24AM
(#14854234)
"I found over 100 if statements that were always true/false or otherwise ineffective."
I'm not sure whether that shows how good Typescript is or how bad Javascript is.
Re:JavaScript for squares: The incredible rise of TypeScript
by slackingoff2017 on Wednesday July 26, 2017 @04:48AM
(#14854518)
Mostly the latter. With strict type checking enabled TsLint will warn you about impossible conditionals. You can go a step farther and force conditionals to return boolean true/false. This disables JavaScript's "truth/falsey" but prevents a whole class of errors caused by questionable design decisions... Like empty arrays being truthy.
I convert using strictest compile rules + TsLint with most rules on. In those 10kish lines I found hundreds of potential bugs. I'm sure at least a third were real.
The biggest culprits in our codebase were classic type errors and improper use of truthy/falsey. Adding types enables a ton of static analysis like identification of broken conditional statements. I found over 100 if statements that were always true/false or otherwise ineffective.