Typed-emitter is a lightweight TypeScript-only package (zero runtime bytes) that provides a strictly typed interface for event emitters. It is currently stable at version 2.1.0, receiving minor updates for bug fixes and feature enhancements, with occasional breaking changes primarily around type constraints or specific integration points like RxJS. Unlike `@types/node` which offers loose typings, or `eventemitter3` which lacks strong argument typing, typed-emitter allows developers to define a precise `EventMap` for their emitter instances. This enables compile-time verification of event names and listener argument signatures, preventing common runtime errors. It's designed to be used with existing `EventEmitter` implementations, such as Node.js's built-in `events` module or browser-based alternatives, without providing its own implementation.
npm install typed-emitterVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining event map types and applying them to a standard Node.js EventEmitter, showcasing compile-time type checking for `emit` and `on` calls.
Ensure all properties within your `EventMap` type are defined as functions (e.g., `(arg1: Type, arg2: Type) => void`).
Import `FromEvent` from `typed-emitter/rxjs` and cast `rxjs.fromEvent` to it: `import { fromEvent as rxFromEvent } from "rxjs"; import { FromEvent } from "typed-emitter/rxjs"; const fromEvent = rxFromEvent as FromEvent;`Upgrade to `typed-emitter` v1.4.0 or newer to ensure correct package resolution and avoid potential build issues.
Adjust the listener function signature (parameter types) to precisely match the `EventMap` definition for the corresponding event.
Add the desired event name and its corresponding listener signature to your `EventMap` type definition.
Ensure that every property (event name) in your `EventMap` is defined as a function type (e.g., `eventName: (arg1: Type) => void`).
No dependency data recorded yet.