DEV Community

Abhirup Datta
Abhirup Datta

Posted on • Updated on

Why typescript is needed?

Continuing on typescript, let's know why typescript is needed.Major two reasons are:

  • Static Type checker
  • Tooling
I will be referring to typescript as "TS" and javascript as "JS" from now on.

Static Type checker

Imagine what happens when we run the following code in JS

function greet(person, date) {
    console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}
greet("Abhirup");
Enter fullscreen mode Exit fullscreen mode

It will throw a typeerror !!

console.log(`Hello ${person}, today is ${date.toDateString()}!`); 
                                              ^
TypeError: Cannot read property 'toDateString' of undefined
Enter fullscreen mode Exit fullscreen mode

Why? Because the date param of greet is undefined and so toDateString of undefined throws error.But we got to know of this error while running only.

Now if we paste the same code in the .ts file.

typescript2
Without running our file we got to know the error(red squiggly line 6) and hence we can quickly fix it.

Tooling

It means first class support of typescript by the popular IDEs and code editors.
Check this link for more.
Whenever we hover mouse on the error "squiggly" lines, we get to know of the potential fix.Also we can "click" on the variable to check the reference etc.


Please do like and share this blog.

Discussion (3)

Collapse
jonrandy profile image
Jon Randy

You don't need Typescript to get the features listed here

Collapse
abhidatta0 profile image
Abhirup Datta Author

Hey, Joe, the above features are not supported in vanilla javascrpt. till date.

Collapse
jonrandy profile image
Jon Randy

I must be from the future then