打字稿,"NodeListOf< Element>"不是数组类型或字符串类型 [英] Typescript,'NodeListOf<Element>' is not an array type or a string type
本文介绍了打字稿,"NodeListOf< Element>"不是数组类型或字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将我的JS转换为TS严格模式.
Converting my JS to TS strict mode.
以下语法对我来说似乎不错,但TS在allSubMenus
的for
循环中抱怨:
The following syntax looks fine to me but TS is complaining in the for
loop on allSubMenus
with:
[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')
}
}
推荐答案
您需要将target
编译器选项设置为es6
或更高,以使NodeListOf<T>
可以迭代.
You need to set the target
compiler option to es6
or higher for NodeListOf<T>
to be iterable.
这篇关于打字稿,"NodeListOf< Element>"不是数组类型或字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文