-
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
Iterating ES6 Map object #6842
Comments
Works fine in |
I meant |
The error message sounds like you're targeting ES5 and forcing it to use lib.es6.d.ts to be able to get |
looks like a duplicate of #4947, or at least stemming from the same issue. |
@Arnavion is right, targeting ES6 solves the problem. I'm now transpiling TS to ES6 and then ES6 to ES5 with Babel. Thank you! |
This seems pretty strange. Why should it be necessary to transpile to ES6 and then again with Babel (the process of which incidentally messes up source maps in our project) just in order to have a standard ES6 feature work? Isn't TypeScript supposed to be a superset of ES6 and the TypeScript compiler a drop-in replacement for Babel? |
TypeScript is a syntactic superset of JavaScript, not a functional superset. When syntax and functionality intersect, there is a bit of a grey area. When targeting ES5, TypeScript assumes that only number indexed based objects (ArrayLike) can be down-emitted with #11209 discusses this in more detail with other possible solutions when you cross the syntactic/functional barrier of ES6. |
Indeed, even though TS emits ES5-compliant JS, that JS is not ES6-conformant. For example, the JS emitted for |
I'd be totally fine if core-js were a requirement. In fact we are using core-js already for exactly this reason. The problem is that it doesn't help much to have those (type-safe!) ES6 collections classes available, if it's not even possible to iterate over them. |
I got the error when using map, even when compiling to mergeAll() {
this.streams.values().map(stream => stream.subscribe(this.stream))
} Worked when I switched to a for (let stream of this.streams.values()) {
stream.subscribe(this.stream)
} Any suggestions? |
Assuming you have a polyfill for newer Array features, one not-awful workaround I've come up with is to use the second const map = new Map<string, number>();
map.set('a', 1);
map.set('b', 2);
Array.from(map.values(), (value: number) => {
alert(value);
}); |
This issue predates the availability of that feature. |
When I try to iterate over a
Map
object as described in MDN: Map, i.e.the compiler complains:
Is this a bug, or am I doing something wrong?
The text was updated successfully, but these errors were encountered: