-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
for..of with iterators #3164
Comments
This is already allowed if your target is ES6: interface MyMap<T> {
values(): Iterable<T>;
}
var myMap: MyMap<string>;
for (let value of myMap.values()) {
var s: string = value;
} The reason why this is not allowed in ES5/ES3 are:
As a result of these factors, in ES3/ES5 only arrays are allowed in for..of loops (as the most common iterable objects available in the JS language today); as for targeting ES6 (i.e. with runtime engine support for iterables and iterable arrays) custom iterables are allowed in addition to Array, string, map and set,..etc.. |
Understood... |
I find this really disappointing. I can use for-of with any iterable and target ES5 with Traceur and Babel today. I'm interested in proposing that our team switch from Traceur to TypeScript, but this limitation in TypeScript will stop that from happening. When TypeScript says it aims to be a superset of ES6, I think that needs to include the ability to target ES5 browsers for all the ES6 features it supports. |
I guess I could use TypeScript to target ES6 and then run that output through Traceur or Babel. I really don't want to have to do that though. |
As an update for this issue, the iterator protocol is now supported for target ES3/ES5 using |
It looks like this is supposed to be fixed for TS 2.3, but I'm running TS 2.3.3 and
where
Am I missing something? |
Ah, indeed. I can't see in this documentation or #12346 - why is this hidden behind an option as opposed to standard behaviour? Will this always remain optional? |
@lhunath it is in the official release information for 2.3. It is optional because it has a very significant impact on the size of generated code, and potentially on performance, for all uses of iterables (including arrays). I find the tradeoff to be worth the increased expressiveness, but making existing code slower and more complex seems like reasonable justification for there being a flag. |
It would be nice if the new sintax
for (let value of values)
works with iterators; ie:Related with #2695 .
The text was updated successfully, but these errors were encountered: