# @prezly/typed-events

> Event emitter with DOM-like EventTarget API & strong TypeScript typings

Latest version **1.0.1** (published 2021-02-22) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @prezly/typed-events
pnpm add @prezly/typed-events
yarn add @prezly/typed-events
bun add @prezly/typed-events
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-02-22 |
| First published | 2021-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | markogresak, kudlik, kamilmielnik, e1npm |

## Links

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

## Recent versions

- 1.0.1 (latest) — 2021-02-22
- 1.0.0-alpha.0 (next) — 2021-02-19
- 1.0.0 — 2021-02-19

## README

# @prezly/typed-events

![Version](https://img.shields.io/npm/v/@prezly/typed-events)
![License](https://img.shields.io/npm/l/@prezly/typed-events)
![Build](https://github.com/prezly/typed-events/workflows/Build/badge.svg)
![Prettier](https://github.com/prezly/typed-events/workflows/Prettier/badge.svg)

Event emitter with [DOM-like `EventTarget` API](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) & strong TypeScript typings.

---

## Alternatives

There's this great [mitt](https://github.com/developit/mitt) package which implements `EventEmitter` interface from node.js but it does not have strong typings. [There's a PR pending](https://github.com/developit/mitt/pull/114) with this feature, but there's been no activity for 7 months. Perhaps in the future we might not want to maintain this `@prezly/typed-events` package. But on the other hand, we should think twice before using a package where last PR got merged over half year ago.

## Example

Demo: https://codesandbox.io/s/prezlytyped-events-demo-2k7pd

```tsx
import Events from '@prezly/typed-events';

const ref = { current: 0 };

const events = new Events<{
    add: number;
    substract: number;
    log: never; // "never" means that event has no payload
}>();

const add = (value: number) => {
    ref.current += value;
};

const substract = (value: number) => {
    ref.current -= value;
};

const log = () => {
    console.log(ref.current);
};

// Attach event handlers
events.addEventListener('add', add);
events.addEventListener('substract', substract);
const removeLogListener = events.addEventListener('log', log);

// Dispatch events
events.dispatchEvent('add', 2);
events.dispatchEvent('substract', 5);
events.dispatchEvent('log');

// Remove event handlers
events.removeEventListener('add', add);
events.removeEventListener('substract', substract);
removeLogListener(); // this is another way to unsubscribe event listener
```

---

Brought to you by [Prezly](https://www.prezly.com/?utm_source=github&utm_campaign=@prezly/typed-events).

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