DEV Community

boopalan
boopalan

Posted on

Why we need TypeScript rather then JavaScript?

What is typescript?
We can say it is simply superset of the javascript that means we can write all the javascript code inside the typescript. Why will this question rise, then why should we use Typescript instead of Javascript. Basically some of the additional features were supported or provided by the Typescript that are not available in the Javascript.

They are Strong Typing, Object Oriented Features, Compile Time Errors 

Typescript was finally converted into Javascript because not all the browsers have the same version of JavaScript. So companies need to support all the browsers not all users use the save Browsers and as well as same platform. To resolve these kinds of issues the Typescript compiler compiles the Typescript code to common code that will run in all the browsers.
Enter fullscreen mode Exit fullscreen mode

How to install typescript

first we need the JavaScript runtime environment in our platform

to check the installation

node -v
Enter fullscreen mode Exit fullscreen mode

to check the node package manager

npm -v

Enter fullscreen mode Exit fullscreen mode

to install typescript in our computer we have to install the typescript package using the node

npm install -g typescript

download and install the typescript globally in our system. So in this way we can typescript anywhere in our computer using the tsc command

tsc -v
Enter fullscreen mode Exit fullscreen mode
// file.js
var teststring  = this is a string;
teststring = 1;
teststring = function (a , b) {return a+b};
console.log(teststring );

Enter fullscreen mode Exit fullscreen mode

This javascript code says that we are unable to use any fixed value for this teststring variable. For this small code this is ok but if I face this kind of problem in the production means literally it can take a long debugging time. So to avoid those kinds of issues we can use the typescript, it will say all the errors in the compile time, it helps to resolve immediately before the production it reduces the debugging time.

To avoid all these conflicts, if we define type for each variable what it should want to be, by this way we do not need to worry about getting any other types. Typescript ensure what type want to be in each variable.

file.ts
var teststring: string  = this is a string;
teststring = 1;
// now it would not all to define any other type rather than the string
teststring = function (a , b) {return a+b};
// same as now it would not all to initialise the function for this variable
console.log(teststring );
Enter fullscreen mode Exit fullscreen mode

let we try to compile this typescript code

tsc file.ts
Enter fullscreen mode Exit fullscreen mode

we can able to see the compile this Typescript code compiler error but it would not stop the compilation, it ensures the same types

how many types in the JavaScript

var mystr:string = test;
var mynum: number = 100;
var mybool : boolean  =true; 

mystr = 100;
mynum = true;
mybool = true; 
Enter fullscreen mode Exit fullscreen mode

If we use a typescript extension installed with texteditor, we can be able to see the error in the editor to resolve them.

Even if we compiled it, the type script compiler will show the errors however the code will be compiled into javascript.
We can test it in the node or any other javascript runtime environments.

Top comments (0)