Typescript is a superset of JS. It’s a programming languge with additinal syntax. TypeScript builds on top of JavaScript. The following pictcure shows the relationship between the two.

First, you write the TypeScript code. Then, you compile the TypeScript code into plain JavaScript code using a TypeScript compiler(tsc).

TypeScript uses the JavaScript syntaxes and adds additional syntaxes for supporting data types. Note that Javascript is dynamically typed languge meaning if there is any error in the code, it’s caught at runtime and the program doesn’t terminate gracefully. Using typescript helps to identify errors at compile time.
why typescript?
typescript adds data-type feature which helps to identify error at compile time. Thus, it forces us to write robust, clean and less error-prone code. For example. let’s say we want to write a function that adds two numbers, num1 and num2. If we write this function in javascript and without any additional validation, we will get unwanted result(string concatination rather than addition). Look at the screenshots below for further illustration.
the screenshot below shows simple index.html page with two input filed and button to add the two numbers.

the screenshot below shows the script.js file where we define our add function.

if we open index.html file and put some numbers in the input field, and press add button. we won’t get the exepected result in the console. Rather we get concatination of string values.

Now by using typescript we can write more robust code and enforce we are getting numbers insted of string values. Thus avoid runtime errors or unexpected results in our case. The screenshot below shows an example of typescript.

Now, we can compile this file by using typescript compiler(tsc) and run the command ‘tsc script.ts’. This command will compile our code and produce ‘script.js’ file. Below is the console output after compiling this code.

Thus using typescript we can write clean and more robust code.
Additional advantage of using typescript
Apart from writing more robust and less-error prone code, typescript has the following advantages
- we can use next generation Javascript features and everyting we write compiles down to js code.
- we can use features like interface and generics that helps during development.
- It has rich configuration options
Disadvantage of using typescript
Although typescript provides lots of feature, one drawback is that neither broswer nor node can execute typescript file. That is why we have tsc tool (typescript compiler) to compile our code back to plain Javascript so that our browser can interpret it.
How to install it?
Inorder to start using typescript we need to install it using npm. Below is a command to install typescript globally.
npm install -g typescript










