FewV minutes of TypeScript and Installing it.
» What is TypeScript?
-
TypeScript is a superset of JavaScript.
-
It can be executed anywhere where JavaScript is supported like in the browser.
-
TypeScript provides all the features and functionalities of JavaScript with some added features.
-
TypeScript compiles to JavaScript, which the browser understands.
-
It was created by Microsoft and is open source.
» Let’s understand with basic example.
- In JavaScript, we can declare a variable of any type without defining it’s type Like this:
var name = 'John'
var age = 27
var rightAge = true
var fullName = {
firstName: 'John'
lastName: 'Doe'
}
-
But while using TS, it is necessary to define the type of a variable whether it is a string, number, boolean, etc.
-
Like this:
let firstName: string = "John";
var myAge: number = 27;
- Now we can’t use firstName as a number or boolean, the TypeScript compiler will throw an error
let firstName: string = false; //error => Type string not assignable to type boolean
var myAge: number = "John"; //error => Type number not assignable to type string
-
Now that is why TypeScript saves a lot of time from debugging and helps write consistent code.
-
Moreover, TypeScript provides various other features, like interfaces, type aliases, abstract classes, function overloading, tuple, generics, etc.
» How to install TypeScript
-
Browsers don’t understand TypeScript.
-
They understand JavaScript code. Hence, the TypeScript code needs to be compiled into JavaScript, and for that, we need the TypeScript compiler.
-
To install TypeScript, we have a command that is
npm install -g typescript
- This above command, we have to run it in command prompt or VS code terminal or Window powershell and it will show us this message
- To check TypeScript version.
tsc -v