# tiny-typed-emitter

> Fully type-checked EventEmitter

Latest version **2.1.0** (published 2021-07-24) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2021-07-24 |
| First published | 2020-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 127 |
| Author | Zurab Benashvili |
| Maintainers | binier |
| Keywords | tiny-typed-emitter, typed-event-emitter, typescript, typescript-library, typescript-boilerplate, nodejs, nodejs-library, event, events, event-emitter, event-emitters, eventemitter, eventemitter3, definitelytyped, typings, definitely-typed |

## Links

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

## 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) — 2021-07-24
- 2.0.3 — 2020-09-19
- 2.0.2 — 2020-06-12
- 2.0.1 — 2020-05-05
- 1.1.0 — 2020-05-04
- 1.0.0 — 2020-05-04

## README

# tiny-typed-emitter

Have your events and their listeners type-checked with [no overhead](#no-overhead).

[![npm version](https://badge.fury.io/js/tiny-typed-emitter.svg)](https://badge.fury.io/js/tiny-typed-emitter)

## Install
  Simply add the dependency using **npm**:
```console
$ npm i tiny-typed-emitter
```
  or using **yarn**:
```console
$ yarn add tiny-typed-emitter
```

## Usage

1. import **tiny-typed-emitter** library:

  ```ts
  import { TypedEmitter } from 'tiny-typed-emitter';
  ```

2. define events and their listener signatures (**note:** quotes around event names are not mandatory):
  ```ts
  interface MyClassEvents {
    'added': (el: string, wasNew: boolean) => void;
    'deleted': (deletedCount: number) => void;
  }
  ```

3. on this step depending on your use case, you can:
  - define your custom class extending `EventEmitter`:
    ```ts
    class MyClass extends TypedEmitter<MyClassEvents> {
      constructor() {
        super();
      }
    }
    ```
  - create new event emitter instance:
    ```ts
    const emitter = new TypedEmitter<MyClassEvent>();
    ```

## Generic events interface
To use with generic events interface:

```ts
interface MyClassEvents<T> {
  'added': (el: T, wasNew: boolean) => void;
}

class MyClass<T> extends TypedEmitter<MyClassEvents<T>> {

}
```

## Compatible subclasses with different events

The type of `eventNames()` is a superset of the actual event names to make
subclasses of a `TypedEmitter` that introduce different events type
compatible. For example the following is possible:

```ts
class Animal<E extends ListenerSignature<E>=ListenerSignature<unknown>> extends TypedEmitter<{spawn: () => void} & E> {
  constructor() {
    super();
  }
}

class Frog<E extends ListenerSignature<E>> extends Animal<{jump: () => void} & E> {
}

class Bird<E extends ListenerSignature<E>> extends Animal<{fly: () => void} & E> {
}

const animals: Animal[] = [new Frog(), new Bird()];
```

## No Overhead
Library adds no overhead. All it does is it simply reexports renamed `EventEmitter`
with customized typings.
You can check **lib/index.js** to see the exported code.

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