🔩 Type-safe event emitter interface for TypeScript
Installations
npm install typed-emitter
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.13.0
NPM Version
8.1.0
Score
98.9
Supply Chain
97.8
Quality
75.7
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
andywer
Download Statistics
Total Downloads
30,940,556
Last Day
19,971
Last Week
133,228
Last Month
1,100,046
Last Year
12,170,170
GitHub Statistics
272 Stars
32 Commits
26 Forks
3 Watching
1 Branches
9 Contributors
Bundle Size
142.00 B
Minified
129.00 B
Minified + Gzipped
Package Meta Information
Latest Version
2.1.0
Package Id
typed-emitter@2.1.0
Unpacked Size
7.17 kB
Size
3.07 kB
File Count
7
NPM Version
8.1.0
Node Version
16.13.0
Total Downloads
Cumulative downloads
Total Downloads
30,940,556
Last day
-20.8%
19,971
Compared to previous day
Last week
-44.3%
133,228
Compared to previous week
Last month
-13.2%
1,100,046
Compared to previous month
Last year
9.9%
12,170,170
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Typed-Emitter
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
1$ npm install --save-dev typed-emitter 2 3# Using yarn: 4$ yarn add --dev typed-emitter
Usage
1import EventEmitter from "events" 2import TypedEmitter from "typed-emitter" 3 4// Define your emitter's types like that: 5// Key: Event name; Value: Listener function signature 6type MessageEvents = { 7 error: (error: Error) => void, 8 message: (body: string, from: string) => void 9} 10 11const messageEmitter = new EventEmitter() as TypedEmitter<MessageEvents> 12 13// Good 👍 14messageEmitter.emit("message", "Hi there!", "no-reply@test.com") 15 16// TypeScript will catch those mistakes ✋ 17messageEmitter.emit("mail", "Hi there!", "no-reply@test.com") 18messageEmitter.emit("message", "Hi there!", true) 19 20// Good 👍 21messageEmitter.on("error", (error: Error) => { /* ... */ }) 22 23// TypeScript will catch those mistakes ✋ 24messageEmitter.on("error", (error: string) => { /* ... */ }) 25messageEmitter.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.
1class MyEventEmitter extends (EventEmitter as new () => TypedEmitter<MyEvents>) { 2 // ... 3}
As a generic class:
1class MyEventEmitter<T> extends (EventEmitter as { new<T>(): TypedEmitter<T> })<T> { 2 // ... 3}
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
:
1import { fromEvent as rxFromEvent } from "rxjs" 2import { default as TypedEmitter, FromEvent } from "typed-emitter/rxjs" 3 4// The `Observable` typing can be correctly inferenced 5const 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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 8/28 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Score
3.3
/10
Last Scanned on 2025-01-13
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to typed-emitter
tiny-typed-emitter
Fully type-checked EventEmitter
emittery
Simple and modern async event emitter
event-emitter-grouped
Emit events in serial or parallel with support for synchronous and asynchronous listeners
@d-fischer/typed-event-emitter
Alternative event emitter for JavaScript and TypeScript.