Skip to content
πŸ”© Type-safe event emitter interface for TypeScript 3
Branch: master
Clone or download
andywer Drop peer depedency
TypeScript shouldn't be a peer dependency, since it is not a runtime dependency. Also: It was misspelled, anyway...
Latest commit 64352ae Sep 27, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Initial implementation, README and metadata Sep 27, 2018
LICENSE
README.md Fix npm badge Sep 27, 2018
index.d.ts Initial implementation, README and metadata Sep 27, 2018
package.json Drop peer depedency Sep 27, 2018

README.md

Typed-Emitter

NPM Version

Strictly typed event emitter interface for TypeScript 3.

Code size: Zero bytes - Just the typings, no implementation. Use the default event emitter of the events module in node.js or bring your favorite implementation when writing code for the browser.

Installation

$ npm install typed-emitter

# Using yarn:
$ yarn add typed-emitter

Usage

import EventEmitter from "events"
import TypedEmitter from "typed-emitter"

// Define your emitter's types like that:
// Key: Event name; Value: Listener function signature
interface MessageEvents {
  error: (error: Error) => void,
  message: (body: string, from: string) => void
}

const messageEmitter = new EventEmitter() as TypedEmitter<MessageEvents>

// Good πŸ‘
messageEmitter.emit("message", "Hi there!", "no-reply@test.com")

// TypeScript will catch those mistakes βœ‹
messageEmitter.emit("mail", "Hi there!", "no-reply@test.com")
messageEmitter.emit("message", "Hi there!", true)

// Good πŸ‘
messageEmitter.on("error", (error: Error) => { /* ... */ })

// TypeScript will catch those mistakes βœ‹
messageEmitter.on("error", (error: string) => { /* ... */ })
messageEmitter.on("failure", (error: Error) => { /* ... */ })

Why another package?

The interface that comes with @types/node is not type-safe at all. It does not even offer a way of specifying the events that the emitter will emit...

The eventemitter3 package is a popular event emitter implementation that comes with TypeScript types out of the box. Unfortunately there is no way to declare the event arguments that the listeners have to expect.

There were a few other examples of type-safe event emitter interfaces out there as well. They were either not published to npm, had an inconsistent interface or other limitations.

License

MIT

You can’t perform that action at this time.