title: Typed events with TypeScript and EventEmitter
body: <p>I'm trying to add strongly typed events to an EventEmitter-like system using TypeScript.</p>
  
  <p>Currently, we define our types like:</p>
  
  <pre><code>interface TypedMsg&lt;Name, T&gt; {
    messageType: Name;
    message: T;
  }
  
  type TypedMsgFoo = TypedMsg&lt;'FOO', string&gt;;
  type TypedMsgBar = TypedMsg&lt;'BAR', number&gt;;
  type EitherFooOrBar = TypedMsgFoo | TypedMsgBar;
  </code></pre>
  
  <p>I would like to define an interface like:</p>
  
  <pre><code>interface EventHandler&lt;T extends TypedMsg&lt;any, any&gt; {
    on: (messageType: T.messageType, handler: (T.message) =&gt; void) =&gt; void;
  }
  </code></pre>
  
  <p>But Typescript doesn't support extracting subtypes like <code>T.messageType</code>.  Is there another way to do this?</p>
  
  <p>The end goal would be to define handlers with proper typing with just:</p>
  
  <pre><code>class FooBarHandler implements EventHandler&lt;EitherFooOrBar&gt; {
    on(messageType: EitherFooOrBar.messageType) {...}
  }
  </code></pre>