Getting Started With TypeScript

Install the TypeScript transpiler using npm:

  1. $ npm install -g typescript

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

  1. $ tsc test.ts
  2. $ 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:

  1. class Pizza {
  2. toppings: string[];
  3. constructor(toppings: string[]) {
  4. this.toppings = toppings;
  5. }
  6. }

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":

  1. class Pizza {
  2. toppings: any;
  3. //...
  4. }

原文: https://angular-2-training-book.rangle.io/handout/features/getting_started_with_typescript.html