title: Typescript extract generic from instance
body: <p>I have a class that uses a generic.</p>
  <pre><code>class Event&lt;P&gt; {
   constructor( public eventName: string ) {}
  
   createPayload( payload: P ) {
     return payload
   }
  }
  </code></pre>
  <p>I would like TypeScript to <em>infer</em> the generic used from the instance, so that I do not have to redefine the type.</p>
  <pre><code>const attackEvent = new Event&lt;{ power: number }&gt;(&quot;attack&quot;)
  </code></pre>
  <p>Since I have defined the generic  here, I would like it to be <em>inferred</em> from the <code>attackEvent</code> instance:</p>
  <p>I have a service that expects a generic, here is an example of the expected use:</p>
  <pre><code>pubSubService.publish&lt;T&gt;()
  </code></pre>
  <p>This is the ideal way that I would like to <em>infer</em> the generic from <code>Event</code>:</p>
  <pre><code>pubSubService.publish&lt; attachEvent &gt;( attackEvent.createPayload({ po}) )
  </code></pre>
  <p><strong>This does not work</strong> , and I understand why. I do not understand how to make something like this work .</p>