Skip to content
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

Can't iterate over DOM elements even with dom.iterable #15236

Closed
seivan opened this issue Apr 17, 2017 · 12 comments
Closed

Can't iterate over DOM elements even with dom.iterable #15236

seivan opened this issue Apr 17, 2017 · 12 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@seivan
Copy link

seivan commented Apr 17, 2017

I might have missed something here. Feel free to close this ticket if it's related to #4947

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

tsconfig.json

    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": false,
        "suppressImplicitAnyIndexErrors": true,
        "module": "commonjs",
        "target": "es5",
        "strictNullChecks": true,
        "experimentalDecorators": true,
        "jsx": "react",
        "lib" : ["dom","es6","dom.iterable","scripthost", "es2015.iterable", "es2015.collection"],
        "alwaysStrict": true,
        "removeComments": true,
        "emitDecoratorMetadata": false,
        "noEmitHelpers" : false,
        "importHelpers" : true,
        "allowJs": true,
        "forceConsistentCasingInFileNames": true,
        "noUnusedLocals": false

    },
const myNodeListOfBrs = document.querySelectorAll("br")
myNodeListOfBrs.forEach(element => {
  console.log(element);            
});

Expected behavior:
It should compile

Actual behavior:
It does not compile with error: property 'forEach' does not exist on type 'NodeListOf<HTMLBRElement>

@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Apr 17, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Apr 17, 2017

PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 17, 2017

Documentation for NodeList.forEach can be found at https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach

@mhegazy mhegazy added this to the Community milestone Apr 17, 2017
@seivan
Copy link
Author

seivan commented Apr 17, 2017

@mhegazy Uhm, But I am not sure if there even is a problem. From what I understand the interfaces exists but maybe you have to import them somehow. I was just trying to figure out what I'm doing wrong.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 17, 2017

NodeList in lib.d.ts needs to have a definition for forEach. it does not today.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2017

Fixed by #14641

@mhegazy mhegazy closed this as completed Apr 20, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Apr 20, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.3.1, Community Apr 20, 2017
@seivan
Copy link
Author

seivan commented Apr 20, 2017

@mhegazy Yeah I know it's been fixed and they exist, I write so in my previous comment. I am just wondering on how to use them? Since they don't actually work out of the box. What do I need to import?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2017

c:\test>type b.ts
const myNodeListOfBrs = document.querySelectorAll("br")
myNodeListOfBrs.forEach(element => {
  console.log(element);
})
c:\test>tsc --v
Version 2.3.0-dev.20170419

c:\test>tsc --lib dom.iterable,es6 C:\test\b.ts

@seivan
Copy link
Author

seivan commented Apr 21, 2017

@mhegazy Thanks, I'll give it a try. It should be available on 2.2.0 as well.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2017

that is a 2.3 change.

@jhpratt
Copy link

jhpratt commented Aug 26, 2017

@mhegazy I'm running tsc 2.3.2 and it fails on the exact example given above.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 29, 2017

make sure your --lib es6,dom,dom.iterable, or tsconfig.json has:

        "lib": [
            "dom", "es6", "dom.iterable"
        ]

@trusktr
Copy link
Contributor

trusktr commented Apr 16, 2018

If I recall correctly, not all browser put a forEach method on NodeLists.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

4 participants