All Projects β†’ andywer β†’ typed-emitter

andywer / typed-emitter

Licence: MIT License
πŸ”© Type-safe event emitter interface for TypeScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to typed-emitter

node-await-event-emitter
await events library like EventEmitter
Stars: ✭ 19 (-89.14%)
Mutual labels:  event-emitter
telephone-ts
Telephone-ts: The "Event Emitter-less" TypeScript Event Architecture.
Stars: ✭ 22 (-87.43%)
Mutual labels:  event-emitter
emitting
EventEmitter designed for TypeScript and Promises
Stars: ✭ 37 (-78.86%)
Mutual labels:  event-emitter
emitter-queue
😴 Queue emitter events
Stars: ✭ 16 (-90.86%)
Mutual labels:  event-emitter
event-emitter
Event Emitter module for Nest framework (node.js) πŸ¦‹
Stars: ✭ 102 (-41.71%)
Mutual labels:  event-emitter
common
Common classes used across prooph components
Stars: ✭ 83 (-52.57%)
Mutual labels:  event-emitter
chokidar-socket-emitter
a simple chokidar watcher which emits events to all connected socket.io clients
Stars: ✭ 28 (-84%)
Mutual labels:  event-emitter
event
πŸ“† Strictly typed event emitter with asynciterator support
Stars: ✭ 30 (-82.86%)
Mutual labels:  event-emitter
tsee
Typed EventEmitter implemented with tsargs
Stars: ✭ 22 (-87.43%)
Mutual labels:  event-emitter
stats
πŸ“Š Collect stats about your node.js process πŸ“Š
Stars: ✭ 29 (-83.43%)
Mutual labels:  event-emitter
EventEmitter
Simple EventEmitter with multiple listeners
Stars: ✭ 19 (-89.14%)
Mutual labels:  event-emitter
tiny-typed-emitter
Fully type-checked NodeJS EventEmitter
Stars: ✭ 96 (-45.14%)
Mutual labels:  event-emitter
parallel-event-emitter
Parallel event emitter built on futures-rs
Stars: ✭ 29 (-83.43%)
Mutual labels:  event-emitter
micro-typed-events
The smallest, most convenient typesafe TS event emitter you'll ever need
Stars: ✭ 39 (-77.71%)
Mutual labels:  event-emitter
Eventemitter2
A nodejs event emitter implementation with namespaces, wildcards, TTL, works in the browser
Stars: ✭ 2,330 (+1231.43%)
Mutual labels:  event-emitter
event-emitter
Simple event emitter for @antvis
Stars: ✭ 14 (-92%)
Mutual labels:  event-emitter
spa-bus
πŸ”₯Tools for multilevel components to pass values in any SPA
Stars: ✭ 15 (-91.43%)
Mutual labels:  event-emitter
zeroin
The only Event Emitter you need
Stars: ✭ 19 (-89.14%)
Mutual labels:  event-emitter
react-bus
A global event emitter for react.
Stars: ✭ 34 (-80.57%)
Mutual labels:  event-emitter
events
Tiny type-safe event emitter
Stars: ✭ 25 (-85.71%)
Mutual labels:  event-emitter

Typed-Emitter

NPM Version

Strictly typed event emitter interface for TypeScript.

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 --save-dev typed-emitter

# Using yarn:
$ yarn add --dev 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
type MessageEvents = {
  error: (error: Error) => void,
  message: (body: string, from: string) => void
}

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

// Good πŸ‘
messageEmitter.emit("message", "Hi there!", "[email protected]")

// TypeScript will catch those mistakes βœ‹
messageEmitter.emit("mail", "Hi there!", "[email protected]")
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) => { /* ... */ })

Extending an emitter

You might find yourself in a situation where you need to extend an event emitter, but also want to strictly type its events. Here is how to.

class MyEventEmitter extends (EventEmitter as new () => TypedEmitter<MyEvents>) {
  // ...
}

As a generic class:

class MyEventEmitter<T> extends (EventEmitter as { new<T>(): TypedEmitter<T> })<T> {
  // ...
}

RxJS fromEvent types inference

The default fromEvent from RxJS will return an Observable<unknown> for our typed emitter.

This can be fixed by the following code, by replacing the fromEvent type with our enhanced one: FromEvent:

import { fromEvent as rxFromEvent } from "rxjs"
import { default as TypedEmitter, FromEvent } from "typed-emitter/rxjs"

// The `Observable` typing can be correctly inferenced
const fromEvent = rxFromEvent as FromEvent

Learn more from rxjs fromEvent compatibility #9 for the fromEvent compatibility discussions.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].