title: Using generics in a for loop in Typescript
body: <p>This is a followup to this question: <a href="https://stackoverflow.com/questions/67995742/maintain-object-value-types-for-each-key-when-reassigning-values/68004406#68004406">Maintain object value types for each key when reassigning values</a>
  This creates an error:</p>
  <pre><code>const obj = {
      a: 1,
      b: 'foo',
  };
  
  for (const k of (Object.keys(obj) as (keyof typeof obj)[])) {
      obj[k] = obj[k];
  }
  </code></pre>
  <p>To fix it, I could use generics and <code>forEach</code>:</p>
  <pre><code>(Object.keys(obj) as (keyof typeof obj)[])
      .forEach(&lt;T extends keyof typeof obj&gt;(k: T) =&gt; {
          obj[k] = obj[k];
      });
  </code></pre>
  <p>Is there a way to use generics with a for loop? I'm not using <code>forEach</code> anywhere else in my codebase and I'd like to keep it consistent. Also, I don't want to define a separate (non-anonymous) function, since <code>forEach</code> would be cleaner.</p>
  <p>TS Playground: <a href="https://www.typescriptlang.org/play?#code/MYewdgzgLgBCBGArGBeGBvAUDHMCGAXDAIwA02u8RA5AGYgjXkC+A3JpvQE4wAUokWAGs4tPgHkkAU2BQAdEKkBPCLwSIAlPgh9FSkGKhKADlINwkGgNoBdDVqy4LiK0Jupnrm+2YdekxBl5PVV1LTwdXj1zI1NzMNsNChw5bgBRPGAAC14AHgAVGCkADygpMAATHWjDEzMxdQA+KKJ8rRRGjGSnTzcPdS92J2YNViA" rel="nofollow noreferrer">https://www.typescriptlang.org/play?#code/MYewdgzgLgBCBGArGBeGBvAUDHMCGAXDAIwA02u8RA5AGYgjXkC+A3JpvQE4wAUokWAGs4tPgHkkAU2BQAdEKkBPCLwSIAlPgh9FSkGKhKADlINwkGgNoBdDVqy4LiK0Jupnrm+2YdekxBl5PVV1LTwdXj1zI1NzMNsNChw5bgBRPGAAC14AHgAVGCkADygpMAATHWjDEzMxdQA+KKJ8rRRGjGSnTzcPdS92J2YNViA</a></p>