Typescript,'NodeListOf<Element>' is not an array type or a string type

This is typescript side parse error. I had same problem with HTMLCollectionOf

make it as any, it works for me

  const allSubMenus : NodeListOf<Element> = document.querySelectorAll('.subMenuItems') 

  for (const sub of allSubMenus as any){ // then will pass compiler
    sub.classList.remove('active')
  }  

You need to set the target compiler option to es6 or higher for NodeListOf<T> to be iterable.


You could try

const allSubMenus : NodeListOf<Element> = document.querySelectorAll('.subMenuItems') 
Array.from(allSubMenus, subMenu => {/* */})