In this article, I will discuss my journey from being an anti-TypeScript developer to a developer who now couldn’t think of going back to the plain JavaScript world. Maybe my thoughts can help someone who is in the same boat as I was in a few years back.
Why I was not interested in Typescript?
I always felt that adding types to functions/variables and satisfying the TypeScript compiler was over-engineering and didn’t provide any meaningful benefits. Also, it felt slow to work on, as I would always get compilation errors that were hard to understand. I would scratch my head trying to figure out the problem. This caused some frustration, and I started hating TypeScript.
The other reason was advanced TypeScript concepts like generics. They were very hard to understand and I started feeling like I was in the Java world, where every piece of code is strongly typed and overwhelming. Even simple code like the snippet below scared me when I started learning TypeScript:
Because of these reasons, even though I was learning TypeScript by watching tutorials or reading books, I never worked on any enterprise application that was written in TypeScript. In fact, I used to choose JavaScript over TypeScript (if there was a choice) for take-home assignments as part of the interview process with companies.
However, when I moved to my current role, working on JavaScript was not an option, as all the apps that I was going to work on were written in TypeScript (with only legacy code in JavaScript). As expected, it was initially overwhelming for me and my hate for TypeScript was increasing. But after a couple of months, I eventually understood the benefits and reasons why someone should prefer TypeScript over JavaScript. I have listed them in the following section.
Top 3 Reasons Why I Became a TypeScript Fan
1. Making impossible states impossible and exhaustive checks
This is the major reason why I love TypeScript. If you would like to know more about this concept, I recommend watching the video below. It talks about the Elm language, but the concept is valid for the TypeScript world as well:
2. Spotting bugs early
While working on JavaScript, I encountered multiple instances where bugs were spotted in production due to some corner case that happened because there was no type checking on the frontend. These bugs can be avoided and caught at compile time by the TypeScript compiler, which will save you hours in the DEV-QA cycle.
With TypeScript, everything stays the way it was initially defined. If a variable is declared as a boolean, it will always be a boolean and won’t turn into a number. This enhances the likelihood of code working the way it was initially intended to. In short, the code is predictable!
3. Rich IDE support and ease of refactoring
Information about types makes editors and Integrated development environments (IDE) much more helpful. They can offer features like code navigation and autocompletion, providing accurate suggestions. You also get feedback while typing: The editor flags errors, including type-related as soon as they occur. All this helps you write maintainable code and results in a significant productivity boost.” — AltexSoft
If we’re talking about refactoring, like introducing a new state or getting rid of an unwanted state that is being used across the app, the TypeScript compiler will complain if you forget to update some references. You can be confident about your refactoring and that the app will work the same way as it did before refactoring.
Conclusion
There are many other benefits to moving to TypeScript (if you haven’t already done so), but these were the main points that made me a TypeScript fan.
Cheers!