DEV Community

Abhirup Datta
Abhirup Datta

Posted on • Edited on

1 1

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.

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

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

Collapse
 
abhidatta0 profile image
Abhirup Datta

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

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay