title: Typescript: Get type of an instance's generic
body: <p><strong>The problem:</strong></p>
  
  <p>Say I have a interface from a third party library that uses generics</p>
  
  <pre><code>interface SomeInterface&lt;T&gt; {
      ...
  }
  </code></pre>
  
  <p>And in my code I have an instance that implements that interface</p>
  
  <pre><code>const someInstance; // type signature: SomeInterface&lt;string&gt;
  </code></pre>
  
  <p>Given this instance, how would I access the type of the generic type parameter T for that instance (in this example, how would I be able to extract the <code>string</code> type from <code>someInstance</code>)? I don't need it for runtime, I just need it so that I can define what type to expect as an argument in a function:</p>
  
  <pre><code>function someFunction(someArg: ???) {...}
  </code></pre>
  
  <p>Basically I want to be able to do this, which doesn't work:</p>
  
  <pre><code>function someFunction(someArg: typeof T in someInstance) {...}
  </code></pre>
  
  <p><strong>Specific use case:</strong></p>
  
  <p>My specific use case here is that I'm using the <code>redux-act</code> and <code>redux-sagas</code> packages. Redux-act provides an action creator factory which produces an action creator with a type signature of <code>ActionCreator&lt;P, M&gt;</code></p>
  
  <pre><code>// someActionCreator has type signature of ActionCreator&lt;string, void&gt;
  const someActionCreator = createAction&lt;string, number&gt;(...); 
  </code></pre>
  
  <p>When this action creator is called via <code>someActionCreator(payload: P, metadata: M)</code>, it produces an <code>Action&lt;P, M&gt;</code>.</p>
  
  <pre><code>// someAction has a type signature of Action&lt;string, number&gt;
  const someAction = someActionCreator("foo", 1);
  </code></pre>
  
  <p>In redux sagas, I have access to the action creator instances (ie <code>someActionCreator</code>), where <code>P</code> and <code>M</code> have defined types, but I don't have access to the actions themselves. However, the handler functions expect the action as the argument, such as</p>
  
  <pre><code>function* someEffectHandler(action: Action&lt;string, void&gt;) {...}
  </code></pre>
  
  <p>Since Typescript knows what the types of <code>P</code> and <code>M</code> are on <code>someActionCreator</code>, I want to be able to access them inside the type declaration of <code>someEffectHandler</code>.
  What I'm trying to avoid is having to write tons of boilerplate for every single action, when the action creator should be able to give me the typed parameters.</p>
  
  <pre><code>//Trying to avoid this
  type SomeActionPayload = string;
  type SomeActionMetadata = number;
  export type SomeAction = Action&lt;SomeActionPayload, SomeActionMetadata&gt;;
  export const someActionCreator = createAction&lt;SomeActionPayload, SomeActionMetadata&gt;(...);
  </code></pre>