-
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 HTMLCollection #5329
Comments
HTMLCollection is not an array. |
xLama, I am aware. However, the compiled code only needs an interface of .length, which means psuedoarrays should also be able to compile with the for..of syntax, right? |
I think If ECMAScript does not support HTMLCollection like an array, TypeScript neither. However ECMAScript 6 supports it, so I am agree. |
That's a good point! |
You can do this por the moment:
|
Yeah. The other thing you can do (if you promise not to mess with the body of the DOM in the loop) is:
|
Thanks! :) |
ES6 does not support HTMLCollection on for-of loop as it is not |
@saschanaz What about this? #2696 |
@saschanaz There is a problem. How do you want to iterate them when emit to ES3/5 without complicate it? |
i think this is something that's missing from i haven't tested other browsers but chrome and firefox both return a function (named not that i know which one i should be reading but mdn provides some spec links for or is this something wrong with the spec? in "living standard" and "dom4" i can see const myElement: Element = document.body;
const children: HTMLCollection = myElement.children;
// is this what i'm supposed to write?
for (const child of <IterableIterator<Element>> Array.prototype[Symbol.iterator].call(children)) {
console.log(child);
}
// it's somehow even longer than the traditional ugly method
for (let i = 0; i < children.length; i++) { const child = children[i];
console.log(child);
}
// this method is cheating but it also works:
interface HTMLCollection {
[Symbol.iterator](): IterableIterator<Element>;
}
for (const child of children) {
console.log(child);
} |
Looking at this again, i believe the TS definitions do match the spec. If the spec is wrong, then we need to change it, but i do not think i have enough DOM expertise to say which is correct. |
Chromium on April 2016 implemented and merged the change based on this criteria. This means we can add |
microsoft/TypeScript-DOM-lib-generator#235 to fix this. |
I see that PR was merged in May, but I still have this error with latest TS (2.4.2) |
@felixfbecker have you tried turning on --downlevelIteration? |
No, didn't know that existed, but as I understand it I don't need it with
when I jump to definition the typings don't have the |
The PR microsoft/TypeScript-DOM-lib-generator#235 added dom.es6.generated.d.ts but TS hasn't merged the file into the build process yet. |
Is there a reason why not the MDN's example of for-of iterator is not working? I have a slight memory that it was working before: var iterable = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return { value: this.i++, done: false };
}
return { value: undefined, done: true };
}
};
}
};
for (var value of iterable) {
console.log(value); // Error: is not an array type or a string type.
} https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of |
You need to compile with |
Worked like a charm, thanks! |
Hi,
Getting a compile error when trying to compile:
I get the following error:
error TS2495: Type 'HTMLCollection' is not an array type or a string type.
Is there a way to get type support for this? Or is too much work to bother with?
The text was updated successfully, but these errors were encountered: