Are HTMLCollection and NodeList iterables?

前端 未结 4 1987
失恋的感觉
失恋的感觉 2020-12-01 23:35

In ES6, an iterable is an object that allows for... of, and has a Symbol.iterator key.

Arrays are iterables, as are Sets and Maps. The question is: are

4条回答
  •  情歌与酒
    2020-12-02 00:14

    For anyone arriving here from trying to iterate on NodeList using TypeScript. I found this issue with the fix https://github.com/microsoft/TypeScript/issues/4947 and this is the tsconfig.json you'll need for it:

    {
      "compilerOptions": {
        "lib": ["es2017", "dom", "dom.iterable"],
        "downlevelIteration": true
      }
    }
    

    The problem error I was getting:

    Type 'NodeListOf' is not an array type.
    

    And this was the code that was triggering that problem:

    [...document.querySelectorAll('#md-view a')]
    

提交回复
热议问题