-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
81 lines (72 loc) · 3.13 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const conventionalEventEmitter = new EventEmitter();
conventionalEventEmitter
.on('foo', (arg: number) => console.log(Math.abs(arg)))
.on('bar', (arg: string) => console.log(arg.length))
.on('baz', () => console.log('baz'));
conventionalEventEmitter.emit('foo', 1); // no error
conventionalEventEmitter.emit('foo'); // no error
conventionalEventEmitter.emit('bar', 'bar'); // no error
conventionalEventEmitter.emit('bar', 1); // no error
conventionalEventEmitter.emit('baz'); // no error
conventionalEventEmitter.emit('baz', 'bar'); // no error
const typedEventEmitter = new EventEmitter<{
foo: number;
bar: string;
baz: void;
}>();
typedEventEmitter
.on('foo', (arg) => console.log(Math.abs(arg)))
.on('bar', (arg) => console.log(arg.length))
.on('baz', () => console.log('baz'));
typedEventEmitter.emit('foo', 1); // no error
typedEventEmitter.emit('foo'); // error
typedEventEmitter.emit('bar', 'bar'); // no error
typedEventEmitter.emit('bar', 1); // error
typedEventEmitter.emit('baz'); // error, unfortunately
typedEventEmitter.emit('baz', void 0); // no error
typedEventEmitter.emit('baz', 'baz'); // error
// Begining of definition
/**
* conventional definition of EventEmitter retained for backward compatibility
* /~https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8c65c84d30d181c36ffd00c77f85181e5350ef61/node/index.d.ts#L263-L278
*/
interface EventEmitter {
addListener(event: string | symbol, listener: Function): this;
on(event: string | symbol, listener: Function): this;
once(event: string | symbol, listener: Function): this;
removeListener(event: string | symbol, listener: Function): this;
removeAllListeners(event?: string | symbol): this;
setMaxListeners(n: number): this;
getMaxListeners(): number;
listeners(event: string | symbol): Function[];
emit(event: string | symbol, ...args: any[]): boolean;
listenerCount(type: string | symbol): number;
// Added in Node 6...
prependListener(event: string | symbol, listener: Function): this;
prependOnceListener(event: string | symbol, listener: Function): this;
eventNames(): (string | symbol)[];
}
/**
* new definition of EventEmitter with keyof
*/
interface TypedEventEmitter<T> {
addListener<K extends keyof T>(event: K, listener: (arg: T[K]) => any): this;
on<K extends keyof T>(event: K, listener: (arg: T[K]) => any): this;
once<K extends keyof T>(event: K, listener: (arg: T[K]) => any): this;
removeListener<K extends keyof T>(event: K, listener: (arg: T[K]) => any): this;
removeAllListeners<K extends keyof T>(event?: K): this;
setMaxListeners(n: number): this;
getMaxListeners(): number;
listeners<K extends keyof T>(event: K): ((arg: T[K]) => any)[];
emit<K extends keyof T>(event: K, arg: T[K]): boolean;
listenerCount<K extends keyof T>(type: K): number;
// Added in Node 6...
prependListener<K extends keyof T>(event: K, listener: (arg: T[K]) => any): this;
prependOnceListener<K extends keyof T>(event: K, listener: (arg: T[K]) => any): this;
eventNames(): (string | symbol)[];
}
interface EventEmitterConstructor {
new (): EventEmitter;
new <T>(): TypedEventEmitter<T>;
}
declare var EventEmitter: EventEmitterConstructor;