import { EventEmitter } from "events" ; hidden export interface EventListeners < T > { newListener : keyof T ; removeListener : keyof T ; } hidden export interface TypedDispatcher < T > { addEventListener < K extends keyof T > ( type , listener ) : void ; addEventListener < E extends keyof EventListeners < T > > ( type , listener ) : void ; removeEventListener < K extends keyof T > ( type , callback ) : void ; removeEventListener < E extends keyof EventListeners < T > > ( type , callback ) : void ; dispatchEvent < K extends keyof T > ( event ) : boolean ; addListener < K extends keyof T > ( event , listener ) : this ; addListener < E extends keyof EventListeners < T > > ( event , listener ) : this ; on < K extends keyof T > ( event , listener ) : this ; on < E extends keyof EventListeners < T > > ( event , listener ) : this ; once < K extends keyof T > ( event , listener ) : this ; once < E extends keyof EventListeners < T > > ( event , listener ) : this ; prependListener < K extends keyof T > ( event , listener ) : this ; prependOnceListener < K extends keyof T > ( event , listener ) : this ; removeListener < K extends keyof T > ( event , listener ) : this ; removeListener < E extends keyof EventListeners < T > > ( event , listener ) : this ; removeAllListeners < K extends keyof T > ( event ? ) : this ; removeAllListeners < E extends keyof EventListeners < T > > ( event ? ) : this ; listeners < K extends keyof T > ( event ) : Array < Function > ; listeners < E extends keyof EventListeners < T > > ( event ) : Array < Function > ; emit < K extends keyof T > ( event , data ) : boolean ; eventNames < K extends keyof T , E extends keyof EventListeners < T > > ( ) : Array < K | E > ; listenerCount < K extends keyof T > ( type ) : number ; listenerCount < E extends keyof EventListeners < T > > ( type ) : number ; setMaxListeners ( n ) : this ; getMaxListeners ( ) : number ; } hidden export class EventDispatcher extends EventEmitter implements EventTarget { private isEventListenerObject = ( listener ) : listener is EventListenerObject => ( listener as EventListenerObject ) . handleEvent !== undefined ; public addEventListener ( type , listener ) { if ( listener ) { const handler = this . isEventListenerObject ( listener ) ? listener . handleEvent : listener ; super . addListener ( type , handler ) ; } } public removeEventListener ( type , callback ) { if ( callback ) { const handler = this . isEventListenerObject ( callback ) ? callback . handleEvent : callback ; super . removeListener ( type , handler ) ; } } public dispatchEvent ( event ) { return super . emit ( event . type , event ) ; } }