将我的 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')
}
}
原文由 Sam 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要将
target
编译器选项设置为es6
或更高的NodeListOf<T>
才能迭代。