Protected

The new protected modifier in classes works like it does in familiar languages like C++, C#, and Java. A protected member of a class is visible only inside subclasses of the class in which it is declared:

  1. ts
    class Thing {
  2. protected doSomething() {
  3. /* ... */
  4. }
  5. }
  6. class MyThing extends Thing {
  7. public myMethod() {
  8. // OK, can access protected member from subclass
  9. this.doSomething();
  10. }
  11. }
  12. var t = new MyThing();
  13. t.doSomething(); // Error, cannot call protected member from outside class

Tuple types

Tuple types express an array where the type of certain elements is known, but need not be the same. For example, you may want to represent an array with a string at position 0 and a number at position 1:

  1. ts
    // Declare a tuple type
  2. var x: [string, number];
  3. // Initialize it
  4. x = ["hello", 10]; // OK
  5. // Initialize it incorrectly
  6. x = [10, "hello"]; // Error

When accessing an element with a known index, the correct type is retrieved:

  1. ts
    console.log(x[0].substr(1)); // OK
  2. console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'

Note that in TypeScript 1.4, when accessing an element outside the set of known indices, a union type is used instead:

  1. ts
    x[3] = "world"; // OK
  2. console.log(x[5].toString()); // OK, 'string' and 'number' both have toString
  3. x[6] = true; // Error, boolean isn't number or string