Learn TypeScript:Part 1 → Introduction.
First part in learning TypeScript.
What is TypeScript ?
- TypeScript is modern JavaScript + types .
- TypeScript is an open-source programming language developed and maintained by Microsoft.
- Typically Typescript is Javascript with types. So that the type error and some of data type related bugs can be resolved .
- It is highly useful in large projects.
Why TypeScript?
In order to understand why typescript is needed, we need to understand what is the problem in Javascript.
What are the problems in Javascript?
In javascript we don’t have strict type in other words javascript is a dynamically typed language which means we can assign any type of value to a variable. The type are automatically assigned during runtime.
Let’s look a practical example why we need to go for TypeScript.
Now what if we for got to pass a number or if we pass string instead of number. What javascript do is , it doesn’t throw any error it result in NaN
This is one of the reasons of many.
So now let’s understand how typescript can be used instead of javascript ,
- We write code in TypeScript(Javascript with types) ,with file extension .ts
- Then we compile the .ts file using the command
tsc filename.ts
. - The file will be transpiled from Typescript to Javascript.
- The we can include the javascript to our webpage.
We write code in Typescript it is converted into javascript by typescript compiler , the typescript compiler will check if there is any type error present , if so it will result in compilation error or else it will produce a javascript file.
Typescript → Javascript + types.
How to install TypeScript.?
You can install typescript by two ways
1 . Via npm (the Node.js package manager)
You need
node
to be installed on your system . you can download it from here .
Once node installed it installs npm(node package manager).
After installing node type the below command to install typescript
npm install -g typescript
It will install typescript in your machine globally.
By installing TypeScript’s Visual Studio plugins
Visual Studio 2017 and Visual Studio 2015 Update 3 include TypeScript by default. If you didn’t install TypeScript with Visual Studio, you can still download it.