Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
Hi,
Getting a compile error when trying to compile:
// ul is an HTMLElement
for (let child of ul.children) {
//body
}
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?
Reacted by Adrian Sampson, Felix Becker, Lucas Basquerotto and Lane Goldberg
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Activity
xLama commentedon Oct 19, 2015
HTMLCollection is not an array.
https://developer.mozilla.org/en-US/docs/Web/API/NodeList#Why_can't_I_use_forEach_or_map_on_a_NodeList.3F
n2westman commentedon Oct 19, 2015
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?
xLama commentedon Oct 19, 2015
I think If ECMAScript does not support HTMLCollection like an array, TypeScript neither.
However ECMAScript 6 supports it, so I am agree.
n2westman commentedon Oct 19, 2015
That's a good point!
xLama commentedon Oct 19, 2015
You can do this por the moment:
n2westman commentedon Oct 19, 2015
Yeah. The other thing you can do (if you promise not to mess with the body of the DOM in the loop) is:
n2westman commentedon Oct 19, 2015
Thanks! :)
saschanaz commentedon Oct 20, 2015
ES6 does not support HTMLCollection on for-of loop as it is not
iterable. NodeList isiterableand should be supported. Check #2695.xLama commentedon Oct 20, 2015
Kagami Sascha Rosylight (@saschanaz) What about this? #2696
for-of only works on arrays for ES3/ES5 by design.
saschanaz commentedon Oct 20, 2015
JCLama (@xLama) You are right. A limited number of
iterableinterfaces should be supported but currently not. #4947 is also related.xLama commentedon Oct 20, 2015
Kagami Sascha Rosylight (@saschanaz) There is a problem. How do you want to iterate them when emit to ES3/5 without complicate it?
13 remaining items
mhegazy commentedon Apr 19, 2017
Looking at this again, i believe the TS definitions do match the spec.
NodeListis iterable, butHTMLCollectionis not. andHTMLElement.childrenis anHTMLCollectionsee https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children.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.
saschanaz commentedon Apr 19, 2017
WebIDL says:
Chromium on April 2016 implemented and merged the change based on this criteria.
This means we can add
[Symbol.iterator]key, and also means that TSJS-lib-generator can automatically add the key but without methods e.g.values().saschanaz commentedon Apr 21, 2017
microsoft/TypeScript-DOM-lib-generator#235 to fix this.
felixfbecker commentedon Aug 15, 2017
I see that PR was merged in May, but I still have this error with latest TS (2.4.2)
RyanCavanaugh commentedon Aug 15, 2017
Felix Becker (@felixfbecker) have you tried turning on --downlevelIteration?
felixfbecker commentedon Aug 15, 2017
No, didn't know that existed, but as I understand it I don't need it with
target: ES6. Here's the error:when I jump to definition the typings don't have the
Symbol.iteratordefined. The flag doesn't help :/saschanaz commentedon Aug 16, 2017
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.
tinganho commentedon Sep 18, 2017
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:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
Here is the code in playground:
http://www.typescriptlang.org/play/index.html#src=var%20iterable%20%3D%20%7B%0A%20%20%5BSymbol.iterator%5D()%20%7B%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20i%3A%200%2C%0A%20%20%20%20%20%20next()%20%7B%0A%20%20%20%20%20%20%20%20if%20(this.i%20%3C%203)%20%7B%0A%20%20%20%20%20%20%20%20%20%20return%20%7B%20value%3A%20this.i%2B%2B%2C%20done%3A%20false%20%7D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20%7B%20value%3A%20undefined%2C%20done%3A%20true%20%7D%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%3B%0A%20%20%7D%0A%7D%3B%0A%0Afor%20(var%20value%20of%20iterable)%20%7B%0A%20%20console.log(value)%3B%0A%7D
kitsonk commentedon Sep 18, 2017
You need to compile with
--downlevelIterationwhen targeting ES3/ES5, which is not supported in the playground at the moment.tinganho commentedon Sep 18, 2017
Worked like a charm, thanks!