Why TypeScript is the Way To Go
I was helping a mentee with some React issues yesterday, and it reminded me why I'm such a big fan of TypeScript. Given my tendency to promote it whenever I can, I thought I'd take a moment and explain why.
Everything has a type
const className = "active"; const elem = $("#app-root"); elem.addClass(className);
In here, we're making a few assumptions:
$object is a function that accepts strings and returns a JQuery object.
elemJQuery object has a function property
addClassthat accepts a string as an argument.
Now consider this TypeScript code:
const className: string = "active"; const elem: JQuery = $("#app-root"); elem.addClass(className);
It does the exact same thing and makes the exact same assumptions, the only difference is that the types are explicitly referenced.
So what? Well, that means there are...
There are some people who think that static languages don't reduce bugs. The problem with analysis made the in article is that it doesn't consider the bugs that were prevented before the code was delivered. I'm talking about bugs like having arguments in the wrong order, misspelled property names, and incorrect type bugs.
I like to call these bugs "that was never going to work anyway" bugs (or just "noooope" bugs). My mentee's bugs were all like that. The code wasn't returning anything, and never would. You'd rarely see those bugs in any package because they'd be detected during testing (human or automated), but that doesn't mean they weren't prevented during the development process.
And reducing the number of "noooope" bugs means that...
Developing becomes more enjoyable
Well, sure! The initial problem is that not everyone is an experienced developer, so they may not know what to look for.
The bigger problem is that when you're developing code of any non-trivial size, the amount of time you spend reading code increases. When you're working with code that isn't explicitly typed, you are taking on more mental responsibility for working out the types involved.
Type annotations save you from the responsibility to work out the types yourself. It makes development easier and more enjoyable because you'd spend less time figuring out what's going on, or debugging "noooope" bugs.
Type annotations (and type checking on compile) also enable huge benefits for developers who use an IDE (and if you don't, why aren't you?). Code completion, documentation lookup, and real-time type checking create huge productivity benefits for developers at all skill levels.
TypeScript + TypeScript-supporting IDEs (Visual Studio Code, Webstorm, etc) save time and make development more enjoyable. Adopt it, and you'll be happy with it.
PS: I've only written a few technical articles before. I'd love to know what you think about this one!