If you are a JavaScript developer, you must have heard about Typescript at one point or another. If you have been afraid about giving Typescript a try because you are not sure how it could serve you better than JavaScript, you have come to the right place.

This guide gives an anterior but absolute guide to Typescript any JavaScript developer would need to get started with it.

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

Note: I may be a bit biased appear Typescript. There’s no activity that I start where I prefer JS to Typescript.

What is Typescript?

You can think of Typescript as a accent that provides an added layer over JavaScript.

Why?

Although we initially write our code in Typescript, we can’t anon run Typescript on a browser like we run JavaScript. Instead, Typescript goes through an added accumulation step to catechumen its code into browser-recognized JavaScript.

So, even when we affairs in Typescript, the end-program that runs on the browser will be in JavaScript.

Then, why do we use Typescript at all?

Though Typescript doesn’t accommodate added functionalities than JavaScript at runtime, it offers a set of appearance to ensure that we, the developers, can write less error-prone and better arguable code compared to when using just JavaScript.

How does Typescript do that?

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

With JavaScript, we can assign an accumulation value to a capricious in the first line and assign a string value to it in the next.

webrok

But with Typescript, we can bind this behavior by absolutely declaring a type for a variable. If we try to assign a string to a capricious of type “number,” it generates an error.

webrok

In a gist, this is what Typescript does abnormally than JavaScript: use types to anticipate us from making silly mistakes in our code.

How Typescript improves upon JavaScript

While the lack of ability to define types is not necessarily a arrears in JavaScript, it gives too much abandon to programmers, which, inevitably, after-effects in them autograph bad code.

webrok

In the above book with Javascript, there’s annihilation to stop the developer from using the aNumber variable to represent an object. While it isn’t an error that would crash the program, it beats the purpose of using capricious names to self-document the code.

Typescript easily solves this issue by defining the type of the capricious during acknowledgment so that it can’t be assigned to a value of addition type.

webrok

If addition developer has access to this capricious in your program, they can now rely upon its value being a number absolutely as the name suggests.

webrok

In this case, the isEligible function expects an object that has a field named age. But Javascript doesn’t have a way to agreement that the altercation passed to the action will be, in fact, an object or it will have a field named age.

Again, Typescript has the band-aid to this problem.

webrok

Now, this code may not make sense to you at the moment. But note how it ensures that the type of the capricious passed is of the type Person, which is authentic at the beginning.

Using Typescript will take away hundreds of absent-minded coding mistakes from your affairs and anticipate you from having to pull your hair out every time you appointment the silliest of bugs. It will also make your code better self-documented and access its maintainability.

If you have been balked with the bereft code suggestions provided for JavaScript in an IDE, then you have addition reason to give Typescript a try. The attendance of types gives Typescript the adeptness to show better code suggestions in an IDE.

Using Types with Typescript

Basic Types

Typescript has a number of basic types that are pre-defined. Number, string, boolean, and array are a few examples of them.

You can find the complete list of basic types in the Typescript documentation.

Here are a few examples:

webrok

Note how any type reverts Typescript to behave the same way as JavaScript. Since our purpose of using Typescript is to give a better anatomy to our code, avoid using any type whenever possible.

Similarly, try to avoid using a union of types, but if it is unavoidable, limit the number of types accustomed in the union as much as possible.

Declaring custom types

Remember how I used a type called Person in a antecedent code example? Person is not a basic data type in Typescript. I created the Person type according to my requirements to use it as the type of constant accustomed by the given function.

We use interfaces to define the basic anatomy of a new type we are introducing to the application.

webrok

Now, if we create a new object of type, it should have the fields name and age within it. If not, Typescript throws an error.

webrok