Improved checking for for..in statements

Previously the type of a for..in variable is inferred to any; that allowed the compiler to ignore invalid uses within the for..in body.

Starting with TypeScript 1.8,:

  • The type of a variable declared in a for..in statement is implicitly string.
  • When an object with a numeric index signature of type T (such as an array) is indexed by a for..in variable of a containing for..in statement for an object with a numeric index signature and without a string index signature (again such as an array), the value produced is of type T.
Example
  1. var a: MyObject[];
  2. for (var x in a) { // Type of x is implicitly string
  3. var obj = a[x]; // Type of obj is MyObject
  4. }