TypeScript is a superset of JavaScript which allows you to write either regular vanilla JavaScript, or, by following the syntactic grammar that TypeScript has developed. It allows you to write classes and functions short hand which is then compiled into robust and scalable JavaScript. If you would like to read through the documentation for typescript you can find it here.
First of all you will need to install TypeScript via the Command Line using Node.js’s package manager. So copy and paste the following code in to install TypeScript globally:
npm install -g typescript
You may need to run the above command as sudo
. Once installed, you can set the watcher command to look for changes on either a specific file or all files in your directory, se blow:
tsc helloworld.ts
or to watch all files:
tsc *.ts
Now that this is done, all of your TypeScript files will compile into pure and clean JavaScript. Now we will look at the syntax and grammar of TypeScript. My favourite is seeing how classes are complied. JavaScript has been moving to a Class based-like method of developing for a little while now by utilising the prototype as a form of inheritance. Now if you were to take the code:
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
var greeter = new Greeter("world");