title: Assigning Properties from one Object to Another
body: <p>How can I get TypeScript to understand that the keys being provided actually belong to the target object when copying properties over from one object to another?</p>
  <pre><code>interface Example {
    value: number
    dontUpdate: string
  }
  
  const a: Example = { value: 1, dontUpdate:  'a' }
  const b: Example = { value: 2, dontUpdate: 'b' }
  
  Object.keys(b).forEach(key =&gt; {
    if (key === 'dontUpdate') return
    a[key] = b[key]
    // ^ Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Example'.
  })
  
  for (let key in b) {
    if (key === 'dontUpdate') continue
    a[key] = b[key]
    // ^ Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Example'.
  }
  
  for (let key in b) {
    if (b.hasOwnProperty(key) &amp;&amp; key !== 'dontUpdate') {
      a[key] = b[key]
      // ^ Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Example'.
    }
  }
  </code></pre>
  <p><a href="https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgKIA84FsAOAbFAbwChlkA3OPAVwgC5kRqsAjaU5AEwHtwBVHJziQGAZzBRQAc2IBfYsQS9xyOAwzZ8KALzJCFKrQYBGADRdeYAUJFkA5HDvJ5SkCpbrMuAsl37KNPTIAEzmPPyCwkF2LE7yxADyLABWEAhgAHQA1hAAnqIAFCwAlBkw3FCoiAAWBTm5vgB8ehzAMMh1eb7aunbhVpGQdsXIUBBg1FAgHHAA2vUAur7ILPN5CxwA9JvIAHpoBFgQ4MjA3sAIwGB4DdVwoqogyA4guU5guTgobAhw1KIoCDoHBjUSiYC8ZDcdofL7PcSSEBSJy-EB2MArFD-CCcZBgbinECcIF4z4oOwabwQOwZOTFBTlKAdAgY+qElYjEhkNodNk9Xr9axRYbIVxgUC0GZrXJLXSrRZbHb7VCHY4Ys74C5XG7IO4POBPF5vUlwn5-AHIIEgiBgiFPaEm8kI6Qog3ozHIbG4-GE4noR3PSlaGlyBkVZnjZBs0Aclrc9pFDJ6hIAdxAAAUoNwvlAPp1ciMAGSFqNdACEPWegsG1M5HDIc0Wy3l63ryG2ewOECOJw1eC111u90ezwNxth3zS5sBwNB4MhDon8IkLtFboxbE9AO9BNAfoDFK8wdpZHksiAA" rel="nofollow noreferrer">Playground</a></p>