Skip to content

Commit cd3a0ff

Browse files
committed
chore: Update README.md
Closes #7
1 parent ce1b41f commit cd3a0ff

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,39 @@ For the API documentation, please follow the official Node.js [documentation](ht
6262

6363
> "error" event is always defined by default because of its different behavior
6464
65+
first create events type (optional)
66+
6567
```typescript
66-
interface Events {
68+
type Events = {
6769
foo: (bar: string) => void;
70+
withContextEnforcement: (this: number, bar: number) => void;
6871
}
72+
```
6973
70-
const eventEmitter = new EventEmitter<Events>();
74+
then create a new direct/extended instance
7175
72-
// or
76+
```typescript
77+
const eventEmitter = new EventEmitter<Events>();
78+
```
7379

80+
```typescript
7481
class Emitter extends EventEmitter<Events> {
7582
}
7683

7784
const eventEmitter = new Emitter();
85+
```
86+
87+
then start emitting & listening to events
7888

89+
```typescript
7990
// Works just fine. so don't worry about "ImplicitAny" config, since type of "bar" is defined as "string"
8091
eventEmitter.on("foo", bar => 1);
8192

93+
// This works fine as well
94+
eventEmitter.on("withContextEnforcement", function (bar) {
95+
return this + bar;
96+
}, 1);
97+
8298
// Throws an error (TS compile time), since this event requires the "bar" argument of type "string"
8399
eventEmitter.emit("foo");
84100

0 commit comments

Comments
 (0)