Getting Started With TypeScript

Install the TypeScript transpiler using npm:

$ npm install -g typescript

Then use tsc to manually compile a TypeScript source file into ES5:

$ tsc test.ts
$ node test.js

Note About ES6 Examples

Our earlier ES6 class won't compile now. TypeScript is more demanding than ES6 and it expects instance properties to be declared:

class Pizza {
  toppings: string[];
  constructor(toppings: string[]) {
    this.toppings = toppings;

Note that now that we've declared toppings to be an array of strings, TypeScript will enforce this. If we try to assign a number to it, we will get an error at compilation time.

If you want to have a property that can be set to a value of any type, however, you can still do this: just declare its type to be "any":

class Pizza {
  toppings: any;

results matching ""

    No results matching ""