Why should you use Typescript?

Why should you use Typescript?

In this article we will discuss typescript and why it is important to use.

Maybe you know the phrase "Typescript is a superset of javascript".

what is that mean?

First of all, we will discuss what javascript does NOT give us.

Javascript is a dynamic programming language or a dynamic data type, which mean the variable or property can hold different data type at runtime.

let user = { name: "Mahmoud", age: 23 };

...
.
....
user.age = "24";
.
..
..
..
.
user.age += 1; // that is will assign 241           

So as we saw in the example above the variable changed data type at runtime and when we add a 1 to the age it will give us an unexpected result because a developer changes the data type to a string.

Maybe this is a simple example but thinks of it as a project ten developer work on it.

we discuss what is the problem with javascript now we will discuss what is typescript.

Typescript is a development tool that gives us static checking.

Development tool means it will doesn't use in production, it uses just in development.

Static Checking will give us a type for variables in javascript like the example below to prevent another developer from changing the data type for a variable or property.

// typescript code
interface User {
  name: string,
  age: number
}
let user = { name: "Mahmoud", age: 23 }

...
.
....
user.age = "24"; // this will give an error 
.
..
..
..
.
user.age += 1;        

By the way, the errors will appear when we write the code immediately.

No alt text provided for this image


Conclusion: Typescript is just a type safety so when you have a big project and has many calculations (processes) you should use typescript. you will write more code but it is type safety, maintainable and scalable.

要查看或添加评论,请登录

Mahmoud Hussam Abbas的更多文章

社区洞察

其他会员也浏览了