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 implicitlystring
. - When an object with a numeric index signature of type
T
(such as an array) is indexed by afor..in
variable of a containingfor..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 typeT
.
Example
var a: MyObject[];
for (var x in a) { // Type of x is implicitly string
var obj = a[x]; // Type of obj is MyObject
}