Why should we all use TypeScript?

TypeScript is a strict syntactical superset of JavaScript. It gives you powerful features which are known in typed languages. It’s hard to go back to writing in JavaScript when you have learned the power of TypeScript. Here are 3 settings in which TypeScript works better than plain JavaScript.

Daniel Koza
Briisk
4 min readMay 23, 2018

--

Joining a new project

Having worked in a few big projects, the ones written in JavaScript and the other ones written in TypeScript, I know that it’s not easy to start working with a big code base, even if it is maintained by experienced developers.

Let’s say you want to see how data flows from an API endpoint to the view. There will always be a service for calling the API, probably another (or something similar) for normalisation, another for syncing with a store/other services, a controller for a component and, at the end, you have an HTML view. So there are at least 4 functions/methods which modify the API data. In plain JS, you need to check every implementation to see how the data changed. With TS you can precisely see the inputs, the outputs and their attributes, of course if types/interfaces are implemented.

It’s easier and faster to write code with TypeScript.

Some say that you can get the same thing with JSDocs, but can JSDocs handle nested types errors? — No. With JSDocs you can specify a generic type (string, number object, boolean) for function parameters. With TypeScript you can specify any interface with many nesting levels.

Types are maintained also by an IDE, so you can see errors while you write a code. If you are a new developer in a new project, it will be easier to understand a code written in TypeScript than in JavaScript.

Maintaining big projects

With TypeScript (and well-written types) you are sure that your function/method arguments are correct, and you will see errors before running the application. This allows you to save time. So paradoxically, writing in TypeScript is faster. Of course, you need to write a lot of code, which will not be executed in a browser, but if you are using an IDE, it will help you a lot with code completion, automatic imports, typos and type prompts.

“Cannot read property ‘name’ of undefined” error is caught before running the application.

TypeScript can work as a part of a documentation. Adding rich types to all functions and methods ensures that all parameters and returned data are already partially described. Later, you can use TSDocs to expand the description.

With TypeScript, you can achieve better code quality (unless you use the `any` type everywhere ;)). JavaScript is a dynamic language, but using it may produce unexpected bugs. In TS you can have dynamic parts and keep everything under control.

Refactoring

Sometimes you need to change the structure of an API response. Without TypeScript, and with dynamic JS features, it’s very hard to check which parts of the code will be affected. Of course, you can have a rich IDE, which will help you. Sometimes, however, it isn’t enough, as there can be tricky parts, where the IDE won’t help, for instance, when checking if a nested attribute exists. Using TypeScript in the project will let you know where incorrect types are used, where attributes have changed and where functions/methods should be updated.

It’s easier to catch errors in the code when the structure of the API response changes.

You can have many unit tests created in a project using JavaScript, but when the payload changes, you need to update mocks in the tests (and probably refactor them a little), and there is no tool which can check if you did it correctly. With TypeScript it is also easier to maintain/refactor unit tests.

Summary

I recommend that you use TypeScript whenever possible, but especially in big projects (with React, Angular, Node… almost every JS framework). The investment (configuration, compilation tax) that you make at the beginning of the project, will certainly pay off later.

--

--