How to fix the ‘property forEach does not exist on type NodeListOf’ error with TypeScript?

Sometimes, we want to fix the ‘property forEach does not exist on type NodeListOf’ error with TypeScript.

In this article, we’ll look at how to fix the ‘property forEach does not exist on type NodeListOf’ error with TypeScript.

How to fix the ‘property forEach does not exist on type NodeListOf’ error with TypeScript?

To fix the ‘property forEach does not exist on type NodeListOf’ error with TypeScript, we should convert the node list returned by querySelectorAll into an array.

For instance, we write

const frameZones = Array.from(document.querySelectorAll("path.frame-zone"));
frameZones.forEach((z) => {
  //...
});

to call Array.from with the NodeList returned by qurySelectorAll to convert the NodeList into an array of nodes.

Then we can call forEach on the array.

Conclusion

To fix the ‘property forEach does not exist on type NodeListOf’ error with TypeScript, we should convert the node list returned by querySelectorAll into an array.