title: Type a function that takes a key and value that match an object type, and then use them
body: <p>I have an object type, and I want to have a function that accepts a <code>key</code> from that object type, and a <code>value</code> that required to be the type of the property that corresponds to <code>key</code>.</p>
  
  <p>I got this <em>mostly</em> working, until you actually use the values, and flow then forgets that they have been required to match up.</p>
  
  <pre><code>// @flow
  
  type Obj = {
    str: string,
    num: number,
  }
  
  const obj: Obj = {
    str: '',
    num: 0,
  }
  
  function foo&lt;KEY: $Keys&lt;Obj&gt;&gt;(
    key: KEY,
    value: $ElementType&lt;Obj, KEY&gt;
  ) {
    obj[key] = value
    //         ^ Cannot assign `value` to `obj[key]` because string [1]
    //           is incompatible with number [2].
  }
  
  // Good
  foo('str', 'good')
  foo('num', 123)
  
  // Bad
  foo('str', 456) // ERROR: as expected.
  foo('num', 'bad') // ERROR: as expected.
  </code></pre>
  
  <p><a href="https://flow.org/try/#0PTAEAEDMBsHsHcBQiAuBPADgU1AeQEYBWoAvKAN6KigDOKATgFy0MCWAdgOYA0Vo7AVwC2zQUPxZ6vAL7IAxrHZ1QsIswLEylanSagA5Pt7UxzAAwy+iSAPZyUrRaEixYAHgDSAUQCazACQeWGg0bhoAfOEAFHwA1sHM3j7GoABuAIbQAlgBXtBYQljsKAAqmFhhRNygSeGIAJQUfKqEANrxaAC6pGmZ2YiyiCCgAOKuACbWrlH6ukYGnBP69VOwM2LzAIwATADMK0NgAEIAggAihy5rswzzACwArABsKyBX68Lz+vjp48tAA" rel="nofollow noreferrer">Try flow link</a></p>
  
  <p>How do I let flow know this is actually just fine, because it <em>just</em> validated that key and value types match up?</p>