for-in statement
Asked Answered
T

4

104

TypeScript docs say nothing about loop like for or for-in. From playing with the language it seems that only any or string variables are supported in for loop.

Why has this decision been made?

Why not use the type information and have strongly-typed iteration variable?

Tailgate answered 18/10, 2012 at 8:51 Comment(0)
E
35

The for-in statement is really there to enumerate over object properties, which is how it is implemented in TypeScript. There are some issues with using it on arrays.

I can't speak on behalf of the TypeScript team, but I believe this is the reason for the implementation in the language.

Electrical answered 18/10, 2012 at 9:11 Comment(0)
D
161

In Typescript 1.5 and later, you can use for..of as opposed to for..in

var numbers = [1, 2, 3];

for (var number of numbers) {
    console.log(number);
}
Diviner answered 9/12, 2015 at 14:47 Comment(3)
for...in iterates the keys of the objects in the collection, while for...of is the correct approach for iterating the objects themselvesInnermost
for..of does not give you an iterator. So, if you're doing something like comparing consecutive values in an array, for..of is not the correct solution, for..in is. TypeScript is WRONG for artificially constraining how these constructs might be used.Distract
just to add to this, if you need the index as well you can do for (var [i, number] of numbers.entries()) { }Non
O
76

TypeScript isn't giving you a gun to shoot yourself in the foot with.

The iterator variable is a string because it is a string, full stop. Observe:

var obj = {};
obj['0'] = 'quote zero quote';
obj[0.0] = 'zero point zero';
obj['[object Object]'] = 'literal string "[object Object]"';
obj[<any>obj] = 'this obj'
obj[<any>undefined] = 'undefined';
obj[<any>"undefined"] = 'the literal string "undefined"';

for(var key in obj) {
    console.log('Type: ' + typeof key);
    console.log(key + ' => ' + obj[key]);
}

How many key/value pairs are in obj now? 6, more or less? No, 3, and all of the keys are strings:

Type: string
0 => zero point zero
Type: string
[object Object] => this obj; 
Type: string
undefined => the literal string "undefined" 
Oreste answered 19/10, 2012 at 21:3 Comment(2)
OK... but a string is not as precise as "a key of this specific object". So how does one type keys of an object when iterating with a for in loop?Metrify
use for...of instead of for...inSwipe
E
35

The for-in statement is really there to enumerate over object properties, which is how it is implemented in TypeScript. There are some issues with using it on arrays.

I can't speak on behalf of the TypeScript team, but I believe this is the reason for the implementation in the language.

Electrical answered 18/10, 2012 at 9:11 Comment(0)
I
24

edit 2018: This is outdated, js and typescript now have for..of loops.
http://www.typescriptlang.org/docs/handbook/iterators-and-generators.html


The book "TypeScript Revealed" says

"You can iterate through the items in an array by using either for or for..in loops as demonstrated here:

// standard for loop
for (var i = 0; i < actors.length; i++)
{
  console.log(actors[i]);
}

// for..in loop
for (var actor in actors)
{
  console.log(actor);
}

"

Turns out, the second loop does not pass the actors in the loop. So would say this is plain wrong. Sadly it is as above, loops are untouched by typescript.

map and forEach often help me and are due to typescripts enhancements on function definitions more approachable, lke at the very moment:

this.notes = arr.map(state => new Note(state));

My wish list to TypeScript;

  1. Generic collections
  2. Iterators (IEnumerable, IEnumerator interfaces would be best)
Indene answered 1/4, 2013 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.