title: Typescript: How to use a generic parameter as object key
body: <p>Is it possible to write an interface that accepts a string constant as one of its parameters, and uses that as the key of an object?</p>
  
  <p>For instance, assuming I make two different GraphQL requests, both of which return a <code>User</code>, but under different key names:</p>
  
  <pre class="lang-js prettyprint-override"><code>const userByIdResult = {
    data: {
      userById: {
         id: 123,
         username: 'joseph'
      }
    }
  }
  
  const userByUsernameResult = {
    data: {
      userByUsername: {
         id: 123,
         username: 'joseph'
      }
    }
  }
  </code></pre>
  
  <p>I would imagine writing a generic interface would go something like this:</p>
  
  <pre><code>interface GraphQLResponse&lt;QueryKey, ResponseType&gt; {
    data: {
      [QueryKey]: ResponseType
    }
  }
  
  interface User {
      username: string
      id: string
  }
  
  type UserByIdResponse = GraphQLResponse&lt;'userById', User&gt;
  type UserByUsernameResponse = GraphQLResponse&lt;'userByUsername', User&gt;
  </code></pre>
  
  <p>But, <a href="https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgOJTgBwBYEUAyAShAM6YD2IJEAPLgK7QCeA0hEwDTLFmXUAqTTBAB8yAN4AoZMgAmcMHABcE6TOQBtBszZMAuip4UqEQcLUBfSVcmhIsRCgCq1KKvXJ6rkHAC2EFRIwKFAAczUZYFlA4LDrSUkwIWdXACEmAElZIz4UAF40DBwCHJMaAHIvaHSs8q4XaBFE5OQGqHS2n39S6mQC9Cw8IlJjagqq9qZOvwg61tcmySA" rel="noreferrer">this doesn't work</a>. </p>