Skip to content

Commit

Permalink
support number as event name
Browse files Browse the repository at this point in the history
  • Loading branch information
Sertonix committed Mar 8, 2022
1 parent 9a139b6 commit 4150571
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type EventMap = {
[key: string]: (...args: any[]) => void
}

type KeyOf<T> = keyof T | `${Extract<keyof T,number>}` | (Extract<keyof T,`${number}`> extends never ? never : number);

/**
* Type-safe event emitter.
*
Expand All @@ -19,22 +21,22 @@ export type EventMap = {
* ```
*/
interface TypedEventEmitter<Events extends EventMap> {
addListener<E extends keyof Events> (event: E, listener: Events[E]): this
on<E extends keyof Events> (event: E, listener: Events[E]): this
once<E extends keyof Events> (event: E, listener: Events[E]): this
prependListener<E extends keyof Events> (event: E, listener: Events[E]): this
prependOnceListener<E extends keyof Events> (event: E, listener: Events[E]): this
addListener<E extends KeyOf<Events>> (event: E, listener: Events[E]): this
on<E extends KeyOf<Events>> (event: E, listener: Events[E]): this
once<E extends KeyOf<Events>> (event: E, listener: Events[E]): this
prependListener<E extends KeyOf<Events>> (event: E, listener: Events[E]): this
prependOnceListener<E extends KeyOf<Events>> (event: E, listener: Events[E]): this

off<E extends keyof Events>(event: E, listener: Events[E]): this
removeAllListeners<E extends keyof Events> (event?: E): this
removeListener<E extends keyof Events> (event: E, listener: Events[E]): this
off<E extends KeyOf<Events>>(event: E, listener: Events[E]): this
removeAllListeners<E extends KeyOf<Events>> (event?: E): this
removeListener<E extends KeyOf<Events>> (event: E, listener: Events[E]): this

emit<E extends keyof Events> (event: E, ...args: Parameters<Events[E]>): boolean
emit<E extends KeyOf<Events>> (event: E, ...args: Parameters<Events[E]>): boolean
// The sloppy `eventNames()` return type is to mitigate type incompatibilities - see #5
eventNames (): (keyof Events | string | symbol)[]
rawListeners<E extends keyof Events> (event: E): Events[E][]
listeners<E extends keyof Events> (event: E): Events[E][]
listenerCount<E extends keyof Events> (event: E): number
eventNames (): (KeyOf<Events> | string | symbol)[]
rawListeners<E extends KeyOf<Events>> (event: E): Events[E][]
listeners<E extends KeyOf<Events>> (event: E): Events[E][]
listenerCount<E extends KeyOf<Events>> (event: E): number

getMaxListeners (): number
setMaxListeners (maxListeners: number): this
Expand Down

0 comments on commit 4150571

Please sign in to comment.