What is TypeScript ?
- Programming language
- Free and open source
- Developed and maintained by Microsoft
Why TypeScript ?
In order to understand why we are shifting our focus to Typescript, we need to have a look into what are the challenges faced in Javascript.
Javascript was developed long back with a focus to do some manipulation in the HTML web pages, however, today Javascript is doing way more than that adding to more complexity.
Issue 1 :Other disadvantages of using javascript is that, there is no restriction to the data type. Lets’s assume var a. Now you can assign a number, string or an object to a. There will be no restrictions to it.
Issue 2 :Other issue in Javascript is with the function arguments. Let’s understand this with help of an example.
In line number 20, the function add is being called with values 1 and undefined, therefore, we get the output as NAN. There is no restriction on the number of parameters that we pass.
In line no. 21, value 3 is being ignored.
Issue 3 :One more problem with Javascript is around objects. In Javascript, you cannot enforce the structure of objects. In Javascript, anyone can add any value/property to the object.
Then Why Javascript ?
If there are so many problems with Javascript, then why do we use Javascipt ? The simple answer for this is because of browsers support only Javascript.
Which means we are kind of stuck. Not necessarily, there is a way to it. We can apply the same idea which is being applied on the High level languages like Java, which are machine independent. Therefore, we can use the concept of compilation to address this issue.
Therefore we will write code in TypeScript, which can be compiled to Javascript and later accepted by the browser.
The compiler is known as the Typescript Compiler.
In simple words TypeScript = Javascript + Type + some other features
TypeScript is a superset of Javascript.
TypeScript contains some syntax features that you can put in the JavaScript code, but they are all optional. It contains all the features of ES5, ES6, ES7 and some additional features as well. As JavaScript is evolving on daily basis, we cannot be sure if it is working on both the older and new versions of browsers. TypeScript takes care of this using TranScript compiler.
Working with TypeScript
So as to work with typescript, you require 3 things:
- IDE – can be Visual Studio Code
- Node.js installed
- TypeScript node package installed globally(npm install typescript -g)