Skip to content

Commit

Permalink
chore: Update README.md
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
ardalanamini committed Jan 22, 2022
1 parent ce1b41f commit cd3a0ff
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,39 @@ For the API documentation, please follow the official Node.js [documentation](ht

> "error" event is always defined by default because of its different behavior
first create events type (optional)

```typescript
interface Events {
type Events = {
foo: (bar: string) => void;
withContextEnforcement: (this: number, bar: number) => void;
}
```
const eventEmitter = new EventEmitter<Events>();
then create a new direct/extended instance
// or
```typescript
const eventEmitter = new EventEmitter<Events>();
```

```typescript
class Emitter extends EventEmitter<Events> {
}

const eventEmitter = new Emitter();
```

then start emitting & listening to events

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

// This works fine as well
eventEmitter.on("withContextEnforcement", function (bar) {
return this + bar;
}, 1);

// Throws an error (TS compile time), since this event requires the "bar" argument of type "string"
eventEmitter.emit("foo");

Expand Down

0 comments on commit cd3a0ff

Please sign in to comment.