Lhew
Lhew

Reputation: 624

document.querySelectorAll on typescript: Type 'NodeListOf<HTMLElement>' is not an array type.ts(2461)

I've been trying a way to get the following on typescript:

    [...document.querySelectorAll<HTMLElement>("#node-a input, #node-a button")].forEach(item => {
//code
    })

But I get the following error:

Type 'NodeListOf<HTMLElement>' is not an array type.ts(2461)

What is wrong with this?

Upvotes: 5

Views: 7254

Answers (1)

downeyfe
downeyfe

Reputation: 106

document.querySelectorAll returns a NodeList, not an array. To treat it as an array you can use Array.from() as suggested, or change your code to expect a NodeList instead. Note that some browsers don't support using forEach() with NodeLists.

Upvotes: 9

Related Questions