title: typescript for loop over HTMLCollection
body: <p>I want to iterate over the results of</p>
  
  <pre><code>document.getElementsByTagName("...");
  </code></pre>
  
  <p>It returns an <code>HTMLCollection</code>, not an array. So I can't simply use <code>forEach</code>.
  The following is possible but doesn't look very nice:</p>
  
  <pre><code>let elements = document.getElementsByTagName("...");
  for (var i = 0, m = elements.length; i &lt; m; i++) {
      let element = elements[i];
  }
  </code></pre>
  
  <p>For javascript, there exists pretty much the exact same question:
  <a href="https://stackoverflow.com/questions/22754315/for-loop-for-htmlcollection-elements">For loop for HTMLCollection elements</a></p>
  
  <p>And apparently, with the latest updates, modern browsers support:</p>
  
  <pre><code>var list = document.getElementsByClassName("events");
  for (let item of list) {
      log(item.id);
  }
  </code></pre>
  
  <p>But the typescript compiler complains:</p>
  
  <pre><code>error TS2495: Type 'NodeListOf&lt;HTMLParagraphElement&gt;' is not an array type or a string type.
  </code></pre>
  
  <p>It still transpiles to proper Javascript though. It's even aware of what I'm doing here and doesn't just copy over the sourcecode. The compiled output is this:</p>
  
  <pre><code>var elements = main_div.getElementsByTagName("p");
  for (var _i = 0, elements_1 = elements; _i &lt; elements_1.length; _i++) {
      var element = elements_1[_i];
  }
  </code></pre>
  
  <p>This is great, since the generated code will be supported even on older browsers. But I would like to get rid of the error message.</p>
  
  <p>My compilerOptions are this:</p>
  
  <pre><code>{
      "compilerOptions": {
  
          "module": "commonjs",
          "target": "es5",
          "sourceMap": true,
          "rootDir": "src",
          "lib": [
              "es2015",
              "dom"
          ]
      }
  }
  </code></pre>
  
  <p>I did try to edit my lib options. This specific feature was a part of ES6 and has been reworked a few times. So I tested the various ecmascript versions in lib but couldn't find a working setup.</p>