Introduction
Hi guys, let’s have a Quick TypeScript tutorial by going through my blog.
What is TypeScript and Why Use It?
It’s an Open Source Programming Language developed & maintained by Microsoft.
Typed super set of JavaScript.
Compiles down to plain JavaScript.
Superior to Dart and Coffeescript languages. It can be easily renamed to .js and works fine.
Optional static typing and type inference.
Adds Type support to JS. Easy error detection in the beginning.
Auto-intellisense prompts you while programming.
Enhanced IDE support.
Rapid growth and use.
Main programming language of Angular and React JS.
Development Environment Setting up

Download and Install Nodejs
Node -v
See the version
Install Typescript globally
Npm install -g typescript
See the version by typing tsc -v
Download and install Visual Studio code from code.visualstudio.com
Create a new path TypeScript folder and enter into it using Nodejs prompt.
Give code . and access into the Visual Studio code editor for your further development activities.
Create a new file main.ts
- let message='Hello Guys';
- console.log(message);
Open PowerShell command explorer
Run the command: tsc main.ts
The above command creates a new file main.js in the Left Navigation Files. Section: main.js which has the below program code:
- var message = 'Hello Guys';
- console.log(message);
Now run the command: node main.js
We have some error in the .TS file. To solve it we need to add an Export/Import command in order to consider it as a module but not a script command.
After Re-Compiling [using same command tsc main.ts] the Error disappears and gets reflected accordingly on the main.js file too.
Run another command: tsc main –watch
Make some changes on your main.ts file
Open another PowerShell Terminal and run: node main
You can see the changed message
Note
TypeScript not allowing special characters in the message like let’s but only lets.
Variable Declarations
Unlike Traditional js, Typescript encourages let and const keywords[support Block level scoping].
How scope and word declarations can trick you in javascript while in TS we have Block scope additional to JS has Global and functional scope
Differences between let and const
Let can be declared without Variable Initialization while const must be declared with a Variable Initialization.
For example, we can have declarations like
Let sum;
Const title; is not allowed as it’s required Initialization and should be constant always!
The above 2 concepts reduced the number of errors in TS programming.
Variable Types
Boolean, Number and String are the types used in TS
- export {}
- let isBeginner: boolean = true;
- let total: number = 0;
- let name: string = 'Bharat';
- let sentence: string = `My name is ${name}
- I am a beginner in TypeScript`;
- console.log(sentence);
Now go to the PS Terminal and run the PS commands,
tsc main.ts
#To create an equivalent main.js file
Node main
You can see the output below:
Null and undefined are classified as subtypes of all other types. It means we can assign the values to them to above Variable Types.
For Ex,
- let isnew: boolean = null;
- let myname: string= undefined;
An Array type can be declared in 2 Syntaxes,
- let list1: number[] = [1, 2, 3];
- let list2: Array < number > = [1, 2, 3];
- // Combination of values in the Array
- let person1: [string, number] = ['Bharat', 31];
Enum Type is a concept of declaring friendly names to a set of numeric values
- enum Color {
- Red = 100, Blue, Green
- };
- let r: Color = Color.Red;
- let b: Color = Color.Blue;
- let g: Color = Color.Green;
- //above list starts numbered from 0,1,2 You can do manual assigning too just as above
- console.log(r);
- console.log(b);
- console.log(g);
You will get the Out put 100,101,102 as index starts from 100 as manually declared above.
If we are unsure about some dynamic value just declare it with any type.
With any type of variable, we can undergo number of errors, like calling it as a function, trying to convert it into Uppercase, console.log with different variable type value, etc. This gives us lots of confusion. TS doesn’t throw Errors.
To tackle this issue TS3.0 has introduced another variable type unknown.
Type Inference is something making the TS self understand the variable value without explicitly declaring it.
For example:


Join the conversation! Your thoughts help the community grow.