首页 > 解决方案 > 打字稿,'NodeListOf' 不是数组类型或字符串类型

问题描述

将我的 JS 转换为 TS 严格模式。

以下语法对我来说看起来不错,但 TS 在for循环中抱怨allSubMenus

[ts] Type 'NodeListOf<Element>' is not an array type or a string type.

我错过了什么?

function subAct(target:Node){

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

  for (const sub of allSubMenus){
    sub.classList.remove('active')
  }  
}

标签: javascripttypescriptiterationnodelisthtmlcollection

解决方案


您需要将target编译器选项设置为es6或更高NodeListOf<T>才能进行迭代。


推荐阅读