You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for the work on this library and for providing type annotations. I would like to suggest an improvement to the type annotations.
Right now, if the type parameter is a map of events and parameter array types, the parameters themselves don't get human-readable names. If the event map type instead supported using entire function annotations, the parameter names could be retained which makes using the library easier.
Without the proposed changes, you could only do the following:
With the proposed changes, you could do this:
Additionally, I would also suggest to add an optional type parameter for the context object.
The type annotations required for this are as follows:
typeValidEventTypes<T=any>=string|symbol|Textends{[KinkeyofT]: any[]|((...args: any[])=>void);}
? T
: never;typeEventNames<TextendsValidEventTypes>=Textendsstring|symbol ? T : keyofT;typeHandler<Textendsany[]|((...args: any[])=>R),R=any>=Textendsany[] ? (...args: T)=>R : T;typeEventListener<TextendsValidEventTypes,KextendsEventNames<T>>=Textendsstring|symbol
? (...args: any[])=>void
: KextendskeyofT
? Handler<T[K],void>
: never;typeEventArgs<TextendsValidEventTypes,KextendsEventNames<T>>=Parameters<EventListener<T,K>>;/** * Minimal `EventEmitter` interface that is molded against the Node.js * `EventEmitter` interface. */classEventEmitter<EventTypesextendsstring|symbol|{[KinkeyofEventTypes]: any[]|((...args: any[])=>void)}=|string|symbol,Contextextendsany=any>{staticprefixed: string|boolean;/** * Return an array listing the events for which the emitter has registered * listeners. */eventNames(): Array<EventNames<EventTypes>>;/** * Return the listeners registered for a given event. */listeners<TextendsEventNames<EventTypes>>(event: T): Array<EventListener<EventTypes,T>>;/** * Return the number of listeners listening to a given event. */listenerCount(event: EventNames<EventTypes>): number;/** * Calls each of the listeners registered for a given event. */emit<TextendsEventNames<EventTypes>>(event: T, ...args: EventArgs<EventTypes,T>): boolean;/** * Add a listener for a given event. */on<TextendsEventNames<EventTypes>>(event: T,fn: EventListener<EventTypes,T>,context?: Context): this;addListener<TextendsEventNames<EventTypes>>(event: T,fn: EventListener<EventTypes,T>,context?: Context): this;/** * Add a one-time listener for a given event. */once<TextendsEventNames<EventTypes>>(event: T,fn: EventListener<EventTypes,T>,context?: Context): this;/** * Remove the listeners of a given event. */removeListener<TextendsEventNames<EventTypes>>(event: T,fn?: EventListener<EventTypes,T>,context?: Context,once?: boolean,): this;off<TextendsEventNames<EventTypes>>(event: T,fn?: EventListener<EventTypes,T>,context?: Context,once?: boolean,): this;/** * Remove all listeners, or those of the specified event. */removeAllListeners(event?: EventNames<EventTypes>): this;}namespaceEventEmitter{exportinterfaceEventEmitterStatic{new<EventTypesextendsValidEventTypes>(): EventEmitter<EventTypes>;}exportconstEventEmitter: EventEmitterStatic;}export=EventEmitter;
Can I submit a PR for this change?
The text was updated successfully, but these errors were encountered:
In case there is any interest, you can avoid dealing with TypeScript definitions here completely, by using sub-events, which can wrap any EventEmitter into a strongly-typed event.
Here's example of wrapping EventEmitter3 into such a strongly-typed event:
import{EventEmitter}from'eventemitter3';import{fromEmitter}from"sub-events/ext";conste=newEventEmitter();// creating our test EventEmitter3, as P.O.C.// Creating a strongly-typed event from EventEmitter3:constevent=fromEmitter<number>(e,'receive');// Now we can subscribe to the strongly-typed event:constsub=event.subscribe(data=>{console.log(data);// data is strongly-typed (number)});// Emitting some data on the source emitter, to test our POC:e.emit('receive',123);sub.cancel();// cancel the subscription when needed
Hi!
Thank you for the work on this library and for providing type annotations. I would like to suggest an improvement to the type annotations.
Right now, if the type parameter is a map of events and parameter array types, the parameters themselves don't get human-readable names. If the event map type instead supported using entire function annotations, the parameter names could be retained which makes using the library easier.
Without the proposed changes, you could only do the following:
With the proposed changes, you could do this:
Additionally, I would also suggest to add an optional type parameter for the
context
object.The type annotations required for this are as follows:
Can I submit a PR for this change?
The text was updated successfully, but these errors were encountered: