What is TypeScript?
TypeScript is not perfect, however. By design, the developers only wanted to add typing to JavaScript. Unfortunately, there are certain weaknesses to JavaScript has, which the TS developers decided to leave without improvement. Currently, TypeScript is a standard for writing modern frontend applications.
Why is TypeScript not suitable for every project?
TypeScript requires some work. First, writing code is slower at first – you need to add types or install various dependencies. In addition, you need to grasp the general workings of TS, what functions has are available, etc. At first glance, using it means introducing an additional tool that… will not change anything in our application. It will continue to look and work the same way. Is it then worth using TS in every project? No. If the purpose of the project is to quickly provide minimal functionality, TS will probably not help and will only slow your progress. It can be assumed as a general rule that when one person is working on a project and the project will not last more than a week, TypeScript will only slow down the work.
What does TypeScript really help with?
So what are the benefits of using TS for other (longer) projects? There are several, which will be discussed below. However, one should note that these are only valid with a good project setup.
First, TS is an extra pair of eyes. It works like an additional programmer who is available almost for free. TS helps catch a lot of borderline cases that JS would overlook. In many instances, TS itself will flag that a certain attribute is not allowed for a certain object and it would be useful to handle the case where that attribute is actually missing. TS also secures the user against typos, for example when referring to fields that have a union type defined.
Many IDEs provide excellent support for TS syntax. This makes writing code faster, as in many cases the IDE itself suggests what values to expect in a given piece of code. We also get real-time feedback on code errors and can quickly get a faster reaction.
Another argument is faster and more precise refactoring. TS analyses the entire application and identifies types that have changed as a result of refactoring. Imagine a situation where we have an API that returns some object in which the id field is a string. Suddenly, for some reason, the API changes and the same field now has the number type. When working in a project without TypeScript, we wouldn’t be able to catch all the places in the code where the API change forces changes so easily. With TS, the compiler will show us all the places where the API change can affect our code. Programmers will find it much easier and faster to catch all the places in the code that need to be corrected. This might seem a very simple example but think of a situation where an API that returns some field as an object later provides something else, e.g. a string or a completely new object.
There’s a lot to be gained if using external libraries. If a particular library used in the project has types, we can adopt into it faster. It’s not necessary to spend so much time searching for information if the types provided by the external tool are well-built.
Probably the last argument, which I however believe to be the most important, is that TS provides code documentation that is always up-to-date. When a new programmer enters a project that uses types, he will be able to guess more quickly what the code does, what the author of the code assumed and what ideas they followed. It is easier to read typed code. By using types, TS creates a priceless layer of documentation that is always up-to-date without additional effort.
More tech articles? Check here!
Summary
TypeScript is a very interesting tool, which has its pros and cons. Weaknesses include more effort required to start using it, as well as extra effort to determine/maintain types. Advantages include the fact that we have an extra pair of eyes, as well as documentation that is always up-to-date for free. In addition, TS helps with refactoring and enjoys great support in IDEs, making it much more efficient to write code and identify code errors faster. From my point of view, TS will benefit any project with several programmers developing it for more than a week. In the case of fast and small projects, the benefits from using TS will not be as clear, so you can forego the tool without any regrets. In any other project, using TypeScript is well worth your while.