title: Self reference key type in a TypeScript interface
body: <p>I have an object with the following interface that I would like to update programatically:</p>
  <pre><code>interface ITypes {
    num: number;
    str: string;
    bol: boolean;
  };
  
  const obj: Partial&lt;ITypes&gt; = {}; // I want to update this programatically
  </code></pre>
  <p>I want to be able to define a <code>key</code> and a <code>val</code> and update my <code>obj</code> using those values. I want it so that the <code>key</code> can only be one of the keys from the <code>ITypes</code> interface, and the value would then be need to be the type specified at the selected <code>key</code> from the interface. I can do this just fine using the below code:</p>
  <pre><code>const key: keyof ITypes = &quot;num&quot;; // key must be a key from the ITypes interface
  const val: ITypes[typeof key] = 1; // val must be the type specified at `&quot;num&quot;` - `number`
  obj[key] = val;
  </code></pre>
  <p>The above code works fine, however, now, instead of having <code>key</code> and <code>val</code> as seperate variables, I want to define them in an object, and so I need to define an interface. However, I'm having trouble with the type definition for <code>val</code>. This is what I have tried so far:</p>
  <pre><code>interface IUpdatePayload {
    key: keyof ITypes;
    val: ITypes[typeof this.key]; // Type 'any' cannot be used as an index type.
  };
  const updatePayload: IUpdatePayload = {key: &quot;num&quot;, val: &quot;1&quot;}; // should complain that `val` is not a number
  obj[updatePayload.key] = updatePayload.val; 
  </code></pre>
  <p>I've tried to self-reference the interface's <code>key</code> type using <code>typeof this.key</code> (which I saw suggested in <a href="https://stackoverflow.com/a/57767027/5648954">this answer</a>) however that is erroring with <code>Type 'any' cannot be used as an index type.</code>, I'm guessing it's because key hasn't actually been defined to have a value like in the first working example using variables. My question is, is there a way to make this work so that <code>val</code> takes on the type defined by <code>key</code> as specified in the <code>ITypes</code> interface?</p>