title: Determine return type of generic function based on function parameter
body: <p>I have a set of Typescript function that look like the following:</p>
  <pre><code>useMyHook = &lt;T&gt;(name: MyType) : T =&gt; {
      // do stuff here
  }
  </code></pre>
  <p>The type of the parameter is a fixed set of strings:</p>
  <pre><code>type MyType = &quot;foos&quot; | &quot;bars&quot; | &quot;bazs&quot;
  </code></pre>
  <p>This all works fine, but it leaves some room for mistakes and it is more verbose than necessary because the parameter &quot;name&quot; and the Type &quot;T&quot; are directly connected and always need to match. If the name parameter is &quot;foos&quot; the generic type T is Foo, if the parameter is &quot;bars&quot; the type is Bar.</p>
  <p>I could create separate strongly typed functions for each type, but the problem is that there is a large number of types and also several different functions. Without going to automatic code generation this would be very tedious.</p>
  <p>As you might guess from the naming this is a custom hook for React, which adds another complication. My intial idea was to create a class to encapsulate the whole set of functions, but this doesn't really work well with how hooks are supposed to be used.</p>
  <p>Type information is not available at runtime in Typescript, so I can't really determine the name parameter from the Type as that information is simply not there. But I was wondering if there is a way in Typescript to represent this kind of dependence between the string passed as parameter and the type of the generic function?</p>
  <p>My goal is that I either have to only pass the parameter and the type is determined automatically. Is that possible with Typescript?</p>