File tree Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -62,23 +62,39 @@ For the API documentation, please follow the official Node.js [documentation](ht
62
62
63
63
> "error" event is always defined by default because of its different behavior
64
64
65
+ first create events type (optional)
66
+
65
67
``` typescript
66
- interface Events {
68
+ type Events = {
67
69
foo: (bar : string ) => void ;
70
+ withContextEnforcement: (this : number , bar : number ) => void ;
68
71
}
72
+ ` ` `
69
73
70
- const eventEmitter = new EventEmitter < Events >();
74
+ then create a new direct/extended instance
71
75
72
- // or
76
+ ` ` ` typescript
77
+ const eventEmitter = new EventEmitter <Events >();
78
+ ```
73
79
80
+ ``` typescript
74
81
class Emitter extends EventEmitter <Events > {
75
82
}
76
83
77
84
const eventEmitter = new Emitter ();
85
+ ```
86
+
87
+ then start emitting & listening to events
78
88
89
+ ``` typescript
79
90
// Works just fine. so don't worry about "ImplicitAny" config, since type of "bar" is defined as "string"
80
91
eventEmitter .on (" foo" , bar => 1 );
81
92
93
+ // This works fine as well
94
+ eventEmitter .on (" withContextEnforcement" , function (bar ) {
95
+ return this + bar ;
96
+ }, 1 );
97
+
82
98
// Throws an error (TS compile time), since this event requires the "bar" argument of type "string"
83
99
eventEmitter .emit (" foo" );
84
100
You can’t perform that action at this time.
0 commit comments