What is Typescript ?
Typescript is a superset of JavaScript. It is an open source language developed by Microsoft which statically compiles the code to JavaScript that can easily run in a browser or in Nodejs.
All the latest features released for ECMAScript are supported in typescript and in addition to it Typescript has its own object-oriented features like interfaces, ambient declaration, class inheritance, etc. which helps in developing a large application which otherwise would be difficult to do in JavaScript.
How to Download and Install TypeScript:
Go to the official site of nodejs : https://nodejs.org/en/download/ and download and install nodejs as per your operating system.
To check if nodejs and npm is installed just check the version in your command prompt.
D:\typeproject>node --version V10.15.1 D:\typeproject>npm --version 6.4.1
So you have nodejs v10 and npm 6 installed.
Typescript Installation
Create your project directory typeproject/ and run npm init, as shown in the command below:
npm init
This will create package.json which will have the store the dependencies for our project.
Once done install typescript as follows:
npm -g install typescript
The above command will take care of installing typescript. Adding “-g” to npm install will install typescript globally. The advantage of using -g is that you will be able to use typescript tsc command from any directory as it is installed globally. In case you don’t want to install typescript globally use below command:
npm --save install typescript
Create src/ folder in your project directory and in src/ folder create typescript file test.ts and write your code. For example:
Example : test.ts
function add(x:number, y:number) { return x+y; } let sum = add(5,10); console.log(sum);