Why Should You Start Using TypeScript?

Krystian Siwek
09 September 2020 · 5 min read

Javascript started as a small scripting language that added some interactivity to web pages. Now it has grown into one of the biggest and most popular programming languages, used for a variety of things: backend, frontend, machine learning, and much more. But JavaScript wasn't created for enterprise-level applications. As your project grows in size and complexity it becomes hard to manage. Luckily Typescript is here to help, designed to support developers via a collection of tools that boost productivity.

What is TypeScript?

TypeScript is a superset of JavaScript programming language. It gives us the option to use static typing, and it compiles to pure JavaScript. Javascript is dynamically typed and variable type is set at the execution of code. TypeScript, on the other hand, is a static type checker that detects errors in code before execution. Also, TypeScript never changes the behavior of your program - when it successfully compiles your code it removes all the information about types, and runs the same as plain JavaScript. In the Stackoverflow Survey 2020 TypeScript ranked second in the list of most loved programming languages, one position higher than its 2019 ranking.

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 need to transpile your TypeScript code to JavaScript because browsers can't interpret TypeScript code. This additional step doesn't require much additional time and can be achieved simply using Babel.

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.

To sum things up, TypeScript is here. It probably isn’t going anywhere. Given time it may become standard to use TS while building JavaScript applications. There are alternatives like Flow, created by Facebook, but they only have small communities. TS is getting bigger and bigger, as we see when comparing the number of NPM installs per month. Now is probably the best time to learn it and put yourself ahead of the crowd, ahead of current trends. Learning TypeScript will teach you exactly how to make your coding easier and more efficient.

Contact us

Related blogposts:

A11y: Web accessibility definition and guidelines

Test automation gains and limitations

GraphQL from Django developer perspective

Why refactoring your code is important?

Share on
Related posts
A11y: Web Accessibility Definition And Guidelines
DEVELOPMENT

A11y: Web Accessibility Definition And Guidelines

Hundreds of millions of people use the internet on a daily basis. It can easily be overlooked, but we should always make sure your website is accessible to everyone, regardless of their disability…
7 min read
Bespoke Software Development: Inhouse or Outsourcing?
DEVELOPMENT

Bespoke Software Development: Inhouse or Outsourcing?

Every company with a brilliant product idea eventually has to answer a fundamental question: should I build it with my own internal team or have it outsourced to a software development company…
6 min read

Talk to us about your project

Get in touch with us and find out how we can help you develop your software
Contact us