A comprehensive guide to Typescript for JavaScript developers

If you are a JavaScript developer, you may have heard of TypeScript at one point or another. If you’re reluctant to give up TypeScript, because you’re not sure how it can serve you better than JavaScript, then you’ve come to the right place.

This guide gives any JavaScript developer an introductory but comprehensive guide to TypeScript that will need to get started with it.

What is TypeScript, what is its type system, and how would it benefit you as a JavaScript developer to use TypeScript in your next project? You will get answers to all these questions by the end of this article.

Note: I may be a bit biased towards typescript. There is no project that I start where I prefer JS to typescript.

What is typescript?

You can think of TypeScript as a language that provides an extra layer on JavaScript.

Why?

Although we initially write our code in TypeScript, we cannot run TypeScript directly on a browser like we run JavaScript. Instead, TypeScript goes through an additional compilation step to convert its code into browser-recognized JavaScript.

Therefore, even when we program in TypeScript, the end-program that runs on the browser will be in JavaScript.

Then, why do we use TypeScript?

Although TypeScript does not provide additional functionalities compared to JavaScript in runtime, to ensure that we, developers, can write less error-prone and better maintain code than when using only JavaScript.

How does TypeScript?

TypeScript, as the name suggests, introduces a type of system on top of vanilla JavaScript. Whereas with JavaScript, the type of variable is dynamically assigned, TypeScript forces us to pre-define the type of variable that we are declaring.

With JavaScript, we can assign an integer value to a variable in the first line and a string value in the next.

But with TypeScript, we can restrict this behavior by explicitly declaring a type for a variable. If we try to specify the string in a variable of “number”, it generates an error.