title: Typescript Generic Transform object from string
body: <p>i want to ask about how to grab string value from parameter to become a key in object transformation</p>
  <p>ex:</p>
  <pre><code>function transformObject(
      value, // &lt;-- any, but for next example i will demonstrate as array
      key  // &lt;-- string
  ) {
      return { [key]: value };
  }
  
  </code></pre>
  <p>hoping to get value like this</p>
  <pre><code>const foo = transformObject(['anyValue'], 'key']);
  foo.key  &lt;-- autocomplete
  
  // foo = {
  //  key: ['anyValue']
  // }
  
  </code></pre>
  <p>so i come up with this idea to grab key as </p>
  <pre><code>function transformObject&lt;T extends string&gt;(
      value: any,
      key: T
  ): { [key: T]: string[] } {
      return { [key]: value };
  }
  </code></pre>
  <p>but it said</p>
  <pre><code>An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.ts(1337)
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <p>enter code here</p>
  <p>already answered from the comment, meanwhile you can open the answer from comment, i also update here to show you on how the code should be</p>
  <pre><code>function transformObject&lt;T extends string&gt;(
      value: string[],
      key: T
  ) {
      return { [key]: value } as { [key in T]: string[] };
  }
  
  const foo = transformObject(['anyValue'], 'key');
  foo.key
  </code></pre>