The benefits of TypeScript and when you should migrate to it
However, as the codebase was becoming always larger and increasingly complex, I started having frequent typing issues. I’m sure most of you know the frustration of receiving the “Cannot read properties of undefined/null” error message, but you have no clue where it might have originated.
any type doesn’t provide much information for both you and the IDE to work with.
sqlToken variable that came seemingly out of nowhere looks like.
In addition to that, other developers that look at your code might go insane trying to figure out what a certain structure is comprised of or how it should really be used, not to mention the struggle of debugging edge cases-related issues.
Although it may seem scary to migrate your codebase to another technology, especially when you are not familiar with it, and most people tend to stay away from it, it is sometimes necessary. In my case, I had to switch to TypeScript two times.
The first project I had to migrate was the database management system, which was an absolute pain to rewrite. First of all, I had to learn the language and how to set up the development environment. Then I went through the whole codebase and found dozens of dumb typing errors and bad practices I didn’t even think about. In fact, when you are not required to invest much mental effort into consciously designing the data structures you will be working with, you tend to pay less attention when writing code.
The second project I had to migrate was a React frontend of a booking system. Fortunately, frontend code doesn’t usually consist of much logic, so it was relatively easy to rewrite in TypeScript. However, it is crucial for an application to handle correctly data exchanges between the backend and frontend. In particular, the most tricky part is agreeing on a common structure, usually JSON, to represent the data inside requests.
When defining such a structure, it’s a good idea to be as strict and precise as possible with types, defining what is allowed and what is not to avoid potentially dangerous edge cases. This is where statically typed languages such as TypeScript come in handy, as they don’t allow you to perform operations — at least directly — that could result in type errors.
tsconfig.json to define the compiler settings.
In the root directory, run the following commands to add TypeScript to the project. Note that you have must have
npm installed on your system.
npm install typescript — save-dev
npx tsc --init
Now you have to edit the newly created
tsconfig.json file according to your needs. To keep the article more concise, I won’t focus on how to do it, but you can follow this simple guide. Anyway, below is an example of
tsconfig.json for a Node.js project:
Congratulations! You have just made your codebase much more robust and reliable with little effort.
There is no one-size-fits-all approach in life. It all boils down to using the right tool for the right job.
I hope you enjoyed this article. If you have anything to add or any question, please share your thoughts in a comment.
Thanks for reading!