# typed-emitter

> Strictly typed event emitter interface for TypeScript 3.

Latest version **2.1.0** (published 2022-01-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install typed-emitter
pnpm add typed-emitter
yarn add typed-emitter
bun add typed-emitter
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2022-01-22 |
| First published | 2016-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 285 |
| Author | Andy Wermke |
| Maintainers | andywer |
| Keywords | event, emitter, typescript, interface |

## Links

- npm: https://www.npmjs.com/package/typed-emitter
- Repository: https://github.com/andywer/typed-emitter
- Homepage: https://github.com/andywer/typed-emitter#readme
- Issues: https://github.com/andywer/typed-emitter/issues
- npm.io page: https://npm.io/package/typed-emitter

## Dependencies (1)

- [rxjs](https://npm.io/package/rxjs.md) *

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2022-01-22
- 1.5.0-from-event (testing) — 2021-11-13
- 2.0.0 — 2022-01-16
- 1.4.0 — 2021-10-14
- 1.3.1 — 2020-09-23
- 1.3.0 — 2020-08-16
- 1.2.0 — 2020-05-27
- 1.1.0 — 2020-05-24
- 1.0.0 — 2020-03-29
- 0.2.0 — 2019-10-21
- 0.1.0 — 2018-09-27
- 0.0.2 — 2016-04-23
- 0.0.1 — 2016-04-23

## README

# Typed-Emitter

[![NPM Version](https://img.shields.io/npm/v/typed-emitter.svg)](https://www.npmjs.com/package/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

```sh
$ npm install --save-dev typed-emitter

# Using yarn:
$ yarn add --dev typed-emitter
```


## Usage

```ts
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) => { /* ... */ })
```

## 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.

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

As a generic class:

```ts
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`:

```ts
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](https://github.com/andywer/typed-emitter/issues/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

---
_Source: https://npm.io/package/typed-emitter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
