Why you should choose TypeScript over JavaScript

atlas holding a globe

You see Atlas holding the globe in the image above and probably ask why. OK then, let me explain. TypeScript is basically “just” JavaScript with types, but in fact, it’s a very powerful superset of JS developed and maintained by Microsoft. And just like the famous Greek titan, it holds JavaScript and actually the whole frontend world on its shoulders, supporting developers in creating and maintaining large, complex projects. TypeScript is a compiler which helps analyze code and catch errors at compile time instead of runtime. It also has a quite smart type inference mechanism which means that the compiler can infer types based on return type of a function or conditions checked before getting to some point in code.

Why TypeScript was created

TypeScript originated from the shortcomings of JavaScript for the development of large-scale applications both at Microsoft and among their external customers.

This is not the only example of technology which tried to solve a problem with large JavaScript projects. There are also: PureScript, Reason, Elm, or ClojureScript.

In general, it doesn’t matter that much which one you choose. The most important question is if it fulfills your needs. Anyway, there are some reasons why TypeScript is worth paying attention to. Let’s take a look at some numbers about JS and TS.

Chart from State of JS with opinions about TypeScript over time

On the screenshot above we’ve got opinions about TypeScript in recent years. In 2016, the sum of “would use again” and “interested” was 59.4%, in 2020 it increased to 87.7%!

Fragment of chart from State of JS about missing things in JavaScript

When it comes to standard JavaScript and the thing developers are missing the most, it’s… Static typing with 52.5% votes! It means that solutions like TypeScript and other similar ones influenced JS developers so much that they want it as a standard approach in JS.

Benefits of using TypeScript

The most important thing about TypeScript is that it provides types. It means that instead of remembering the type of each structure in the codebase, we can just write it. It’s essential when a project is getting large and will be maintained in the future.

Big projects have a tendency to change developers working on it over time. TypeScript takes responsibility for doing part of their work.

Apart from that, it’s easier to refactor the code, provide big changes and estimate the time they may take. Last but not least, it’s also easier to debug the parts of the code that are not working.

How much it costs

“With great power there must also come great responsibility”

Its downside is quite logical: it needs to be learned. Learning of course takes time and we should be aware of that. Luckily, as it was shown before, TypeScript gets more and more popular and it slowly begins to be a standard in bigger projects.

Another thing is, it takes a bit more time to develop things — we need to think about types, model structures and write additional code.

Is TypeScript mature enough to use it?

Initial version of TypeScript appeared in late 2012. Some time from that moment has passed. Awareness of this technology has increased which was shown on screenshots from State of JS above. Also the number of features, support for libraries and IDEs is way better than before.

Interesting thing about the team behind the TypeScript project is that its core developer is Anders Hejlsberg. He is a lead architect of C# and a creator of Delphi and Turbo Pascal. All of these technologies were quite important in the history of software development.

Migrating JavaScript project to TypeScript

As it was mentioned in the beginning, TypeScript is a superset of JavaScript. It means that every JS project is a valid TS project. In practice, after preparing a TS compiler and basic configuration, the migration process can begin. And it doesn’t need to be finished at once. It can be done in an incremental way. One possibility is to create new files with .ts extension or .tsx in case of React components.

What projects is TypeScript suitable for?

Actually, the title of this article suggests an answer. And here are some details about that thesis. Basically, it’s definitely worth considering in each project that is going to be maintained in the future. In the beginning the profit might be hard to notice, but after some time it definitely pays off.

At iteo we don’t use JavaScript anymore. We’ve decided to fully switch to TypeScript, and we are really happy with our decision. Maintaining big projects became much easier and we’re definitely not the only company which made this decision.

On the other hand, plain JS may still be a good option for doing proof of concept or writing small pieces of code.

human-centric software design & development. check out our website: www.iteo.com