TypeScript Classes

TypeScript also treats classes as their own type:

  1. class Foo { foo: number; }
  2. class Bar { bar: string; }
  3. class Baz {
  4. constructor(foo: Foo, bar: Bar) { }
  5. }
  6. let baz = new Baz(new Foo(), new Bar()); // valid
  7. baz = new Baz(new Bar(), new Foo()); // tsc errors

Like function parameters, classes sometimes have optional members. The same?: syntax can be used on a class definition:

  1. class Person {
  2. name: string;
  3. nickName?: string;
  4. }

In the above example, an instance of Person is guaranteed to have a name,and might optionally have a nickName

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