Recently I was asked why do I love Typescript so much and although I knew in my guts that I love it I couldn't put in words what I knew. For that reason, I decided that to summarize everything that I like and don't like on Typescript. Let's look at a small pros and cons list that we created, we will get into details about each one of those latter.
|Pros ✅||Cons ❌|
|Typing||Extra build step|
|Refactoring and maintenance||Smaller talent pool|
|Code navigation and autocompletion||Learning curve|
|Do not break legacy JS|
The most obvious pro of typescript is like the name indicates the types. With a strong type of implementation not only we can detect an error during the development it also easier to know the issue and fix knowing exact at any time what you need to pass or receive on the codebase.
Refactoring and maintenance
On large teams and projects that have a high rotation of different developers, it makes it much easier to refactor and maintain the codebase because the code is readable and we know exactly what type is each one of the variables and the return of each one of the functions.
This will lead to an increase of predictability on the code because you know exactly what you have at any moment, no more errors of trying to read the length of undefined example among several other operations that make our daily life in a JS project.
Code navigation and autocompletion
With typescript and if you use vscode that's my IDE of selection you will get a lot of benefits like autocomplete on the objects because it knows that inside of the props it has the value X and Y. Another awesome part is the navigation between the files when you click on a function or types allowing you to get a quick overview or open the file entirely.
Do not break legacy JS
One of the major fears of developers, when they decide to migrate into typescript, is that the old JS code would break, of course, this need to be extra careful and the approach that I always recommend is to write the new components in typescript and start migrating slowly.
any that allow anything to be assigned but please use it with care because the usage any it defeats the whole purpose of using typescript.
Now with modern JS, we have a bunch of features that we didn't have in the past like async/await or even even a simple
array.map was a nightmare before ES6 came out. All those features arrived at the TS community first and the compiler converted the language to the desired
ECMAScript. At the current date, several features are still only available on TS like
Extra build step
For developers that have been working on JS or another non-typed language for years, it might look strange and require some learning to see the benefits and use typescript at his full potential. From my experience, one of the hardest concepts to understand for beginners are generics and how to create your .d.ts files with your types for the cases that you don't have any.
Typescript strength is also one of his weaknesses, the code is more readable but that comes at the cost of verbosity. For the same file on average, you will have to write 25% more lines of code to define all your types and expected behaviour. This might look like a waste of time but keep in mind that these extras lines of code will save you time debugging and increase the team performance overall. In the end, it's not that much of Cons.
Smaller talent and projects pool
As someone that has been pushing the idea of typescript since 2015, I can recognise that there's still some resistance from the devs to accept this concept and as result, there's also a lack of projects using typescript and also not a strong push to migrate the code. Luckily in the last couple of years, I can see more and more adoption from new projects and devs learning to love and pushing the boundaries of typescript further making it one of the most loved language according to the state of js 2020.