What is TypeScript?
What are the benefits of TypeScript?
Optional static typing
This is the main feature we use TypeScript for. Static typing means that once you declare variable type, it doesn't change. It can take only certain values. If an error occurs, the compiler warns you so you don’t make a mistake.
More readable and self-documenting code
Adding types makes your code more self-explanatory, which is helpful for large teams and can make up for a lack of communication. This also makes it easier for junior developers to see the intention behind other people's code.
Spotting bugs earlier
Developers can spot a lot of common bugs during the compiling phase, saving time on searching for them later. Some companies even report a reduction of bugs after they switch to TS!
It makes refactoring faster
When it comes to refactoring, TypeScript is enormously helpful in keeping the code consistent, thus avoiding changing the behavior of the code. It automatically finds mistakes and alerts us about the issue, which simplifies the refactoring process.
It makes code easier to merge
After introducing TypeScript to your project, it’s easier to trust new code from other developers. This makes code reviews and the whole process of merging faster.
What are the cons of TypeScript?
Takes some effort to set up
When starting a new app you have to set up your bundler and test runner to work with TypeScript. Also, when adding new libraries you need to install typedefs for them. But this is well worth the effort for the excellent options it provides.
More code to write
If you want to use the full potential of TypeScript, you will need to write more code. This could be interpreted as slowing down the development process.
The extra step of transpiling
You have to spend some time learning it
To start coding with TypeScript, first you have to spend some time getting to know it. Not only the semantics but also good practices like how to use it to structure a project correctly.
When should you use TypeScript?
One of the obvious answers to this question is - when you have a large codebase. When dealing with a big codebase it's easy to get yourself lost in the code. With really advanced IDE support for TypeScript, you can avoid that. IDE support makes it easier for new and more experienced developers to navigate a large project, speeding up the developing process with features like autocompletion and snippets.
Another case when TypeScript comes in handy is when you are dealing with a large team of developers. It saves a massive amount of time that people would otherwise spend understanding the code. In combination with frameworks like Angular and Vue, it makes the codebase consistent and minimizes the risk of someone misusing your code.
While static type checkers shouldn't replace unit tests, sometimes you have tight deadlines and don't have time to write them. Compile-time checking delivers more confidence in situations like this. But remember - TypeScript should never be a substitute for tests.
Last but not least, TypeScript can be very helpful when you are working with an external API. You can use it to define the interfaces of objects that are returned from the API, making it more clear for everyone, and when the JSON response changes it’s easy to find out what’s gone wrong.
And when you should think twice?
When you are working on your small, pet project TS may be overkill. When working alone on a small project, TypeScript probably wouldn't benefit you much. You are probably familiar with codebase already, and the boost of productivity TS brings would be marginal. On the other hand, a pet project is a good place to play with TypeScript if you are not experienced in it, because you don’t have to worry about deadlines.
TypeScript can boost productivity. But if your team is experienced, already following test-driven development, switching to TypeScript in a short term may not be beneficial enough to spend time on configuring it, or introducing it to your developers. Having said that, in the long term the pros of TS speak for themselves.
Should you migrate to TypeScript?
If we are talking about changing your habits and starting to use TypeScript in your new projects, the answer is almost always ‘yes’. As far as projects that are already in progress go, it depends.
One of the situations where you don’t need TypeScript from the start is when you’re writing an app or website that doesn't operate on data, for example a company landing page. Using TS in this context would be pretty useless. Another case might be when the framework you want to use doesn't support it.
What about switching during an in-progress project? If you constantly come across problems with refactoring, keep getting lost, or current tools can't help you, you probably should consider a migration. Of course, you don't have to rewrite the whole project. You can do it file by file, starting with the essentials. This won’t hurt you too much time-wise.