-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.d.ts
43 lines (38 loc) · 1.54 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
export type EventMap = {
[key: string]: (...args: any[]) => void
}
/**
* Type-safe event emitter.
*
* Use it like this:
*
* ```typescript
* type MyEvents = {
* error: (error: Error) => void;
* message: (from: string, content: string) => void;
* }
*
* const myEmitter = new EventEmitter() as TypedEmitter<MyEvents>;
*
* myEmitter.emit("error", "x") // <- Will catch this type error;
* ```
*/
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
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
// 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
getMaxListeners (): number
setMaxListeners (maxListeners: number): this
}
export default TypedEventEmitter