Getting started with TypeScript(TS)

In this article we will gonna learn some basics of TypeScipt.

  1. Installation of TypeScipt
  2. Introduction of TypeScipt
  3. Getting started with TypeScipt
    • Basic data type

So let’s start…….

1.Installation of TypeScipt

Step 1: Install node.js https://nodejs.org/en/download/ . Node basically provides server side environment. By installing node we will get Node Package Manager(NPM).

Npm helps us to  install packages. Package is basically javascript file library which can be use in your project.

If you want to know node is installed or not then open your CMD and type following command
C:\Users\AM\Desktop\TypeScript>node
>

If you want to know version of node then
C:\Users\AM\Desktop\TypeScript>node -v
v10.15.1

step 2: Now it’s time to install TypeScript
C:\Users\AM\Desktop\TypeScript>npm install -g typescript
Above command will install latest version of TS at global level. If you wants to install it locally(i.e for folder only) don’t use -g . To install older version use
C:\Users\AM\Desktop\TypeScript>npm install -g typescript@1.7.5

To know TS version use following command
C:\Users\AM\Desktop\TypeScript>tsc -version
Version 3.3.3333

step 3: We need some editor to write our code. Here i am using Visual Studio Code

2.Introduction of TypeScript

->TypeScript is a language.It’s a super set of JavaScript.
->Ts can be used where you want to write JavaScript.
->When compiled it generates JavaScript.
->TS provides compile time environment where JS lacks. Which means less exceptions or bugs.

->ECMAScript 6 is also called as  ES6 or ECMAScript 2015 or JavaScript 6. ->TypeScript is Extension of ES6
-> We can say JS(parent)->ES6(child of JS)->TS(child of ES6||JS6)

How Typescript make life easier???
practis.js:
function division(a, b) {
return a > b;
}
console.log(division(2,1));

O/P:
C:\Users\AM\Desktop\TypeScript>node practis.js
true

In the above example we have written simple javascript function which accepts two parameters compare it and returns the result. here we have pass two numbers.Let’s see what happens when we pass different type of parameter.

function division(a, b) {
return a > b;
}
console.log(division(2,"1"));

O/P:
C:\Users\AM\Desktop\TypeScript>node practis.js
true

When we pass one number and another string it also gets compared and result is true. This is wrong we can’t compare number with string. In the JavaScript we can’t specify data type of parameters. But in TypeScript you can specify type of arguments you want to accept. let’s see an example.
practice.ts:

In the above example we have written function which will accept two parameter both of type number. If you try to pass string it will throw an error.

We are all set 😉 Let’s start with TypeScript Programming…..

practis.ts
let a="Welcome to JustCompile"
console.log(a);

We have initialized one variable which stores string value. Next line print’s value of variable. Save file with .ts
Run that file with command
tsc practis.ts
after compilation we will get new Javascript file i.e practis.js.
here is the generated js file:
practis.js
var a = "Welcome to JustCompile";
console.log(a);

Now we have to execute this js file with
node practis.js

o/P:
Welcome to JustCompile

To be continued……. 😉

Please follow and like us:
Sending
User Review
1 (1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *