What is TypeScript?
TypeScript is developed by Microsoft and it is a superset of JavaScript; any valid JavaScript code is also a valid TypeScript code.
TypeScript has additional features which do not exist in the current version of JavaScript supported by the latest browsers. For example, TypeScript supports strong typing which does not exist in JavaScript. Using strong typing we can avoid programming mistakes and errors at compile time and we can figure out issues at compile time. TypeScript also supports specific object oriented features which are not in JavaScript like classes, interface, access modifier, fields, properties and generics. We also get intellisense support while developing code using TypeScript. TypeScript code is compiled, and after compilation its equivalent JavaScript code is generated. Browsers do not understand TypeScript code, so we need to compile TypeScript code for generating the equivalent JavaScript and browsers can thehn execute that JavaScript code.
How to Install TypeScript
Now we will see how to install TypeScript and write a TypeScript program.
We need to install TypeScript globally on our machine.
Open Command Prompt and type the following code.
npm install -g TypeScript
Here ‘g’ stands for global
For checking the current version of TypeScript which is installed, we can write the following code:
tsc --version
‘tsc’ stands for TypeScript.
This command will give you the current installed version of TypeScript. You can see the current version on my machine is 3.1.3.
Create First TypeScript Program
First, you need to create a directory for our TypeScript project.
Open command prompt and write the following code,
code welcome.ts
Press enter, and the VS code window will appear like below,
VS code is a cross platform code editor. You can install VS code from below link,
https://code.visualstudio.com/
We will write Javascript code in this file to verify that javascript code is also valid TypeScript code.
Write the below code in welcome.ts file,
Now we compile it. Use the below code to compile
tsc welcome.ts
After compilation a new file, welcome.js, will be generated. We can execute using the below code,
node welcome.js
For executing javascript files, we need to install Node.js. You can see my below article for installing Node.js on your machine.