Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
typed-emitter
Advanced tools
Readme
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.
$ npm install --save-dev typed-emitter
# Using yarn:
$ yarn add --dev typed-emitter
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!", "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) => { /* ... */ })
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> {
// ...
}
fromEvent
types inferenceThe 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.
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.
MIT
FAQs
Strictly typed event emitter interface for TypeScript 3.
The npm package typed-emitter receives a total of 222,647 weekly downloads. As such, typed-emitter popularity was classified as popular.
We found that typed-emitter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.