What makes TypeScript important to learn?
Tags: javascript, typescript, react
Published: 2022-11-28 14:27:10

**Today we’ll see why you should learn TypeScript. What are the benefits of TypeScript?**

Hii! my frontend -friends!

If you’re unsure whether to learn typeScript or not, you’ve come to the correct spot. You won’t need to read any more after reading this one since you will understand exactly what typeScript can achieve for you.

Let’s use examples to clarify each point in turn.


## Static type-checking

Typescript is mostly used to prevent runtime errors. We can add static type checking to our code by using typescript, which is so incredible.

Let’s take an example

```
let message = 'Helllo!';
message(); // In JavaScript you'll get error in runtime

// But in TypeScript

let message = 'Helllo!';
message(); // While writing this code, you'll be alerted right away.
```


![ts throwing error while calling string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fjjllrm6oj7wxlmogmv.png)
ts throwing error while calling string


Here, we first declare the message to be a string before calling it since, as we all know, a string cannot be called. (Large projects may make it difficult to remember the type of a variable (string, function, etc.), so we frequently make mistakes with types.)

The runtime in JavaScript will identify this issue and throw it. But in typescript, TypeScript will give us an error message before we run the code in the first place. it’s super amazing you don’t have to wait for the runtime to find the error

## Non-exception Failures
In general, if we are trying to access something that is not defined we should get an error. but you could imagine that accessing a property that doesn’t exist on an object should throw an error too. Instead, JavaScript gives undefined that sometimes break our code.

Let’s take an example

```
const person = {
  name: 'joe',
  age: '26'
}

person.location // In javascript you'll get undefined
// But in typescript
person.location // You will be informed right away.
```
Here in person object, we are trying to access the property that is person.location which is not defined.

In javascript, you’ll get undefined that sometimes breaks our code.
But in Typescript, you’ll be informed right away.

![When attempting to access a property that is not defined, ts throws an error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2bgyy2kercdt3m2r8sr.png)
When attempting to access a property that is not defined, ts throws an error.

## Typos
Typescript also prevents typos


```
let message = 'Hello World!'

message.toLocaleLowercase();
message.toLocaleLowerCase();
```


![ts throwing an error while adding a property that does not exist](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epd3ppqo4dcun2bkkrso.png)
ts throwing an error while adding a property that does not exist.


How quickly can you find the typos?.. typescript helps us to avoid such typos.

## Explicit Types
So far, we haven’t told TypeScript about variable types, such as “variable must be a string, number, boolean, etc.”

Let’s understand it by an example

```

function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}

```

here we’ve added the type annotations on person and date to describe what types of values greet can be called with. you can understand it as a person of type string and a date of type Date .

With this, TypeScript can tell us about other cases that greet might have been called incorrectly. For example…

```

function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}
 
greet(true, new Date());
// Argument of type 'boolean' is not assignable to parameter of type 'string'
```

![ts is throwing an error while passing a boolean to the typed string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpf3fbyvf6nsrje09i61.png)

ts is throwing an error while passing a boolean to the typed string

## tsc, the TypeScript compiler
Browser does not understand typescript so we have to compline it into js by using tsc compiler

`npm install -g typescript`

you can compile your file

`tsc <your file name>`

Hope you like it 🤗

Happy coding!

want to give suggestions:-

find me on [LinkedIn](https://www.linkedin.com/in/%F0%9F%91%A9%E2%80%8D%F0%9F%92%BB-nidhi-sharma-55329823b/) [Twitter](https://twitter.com/NidhiSh57914602)

Email me at nidhisharma639593@gmail.com