# simple-typed-events

> Typed event emitter

Latest version **1.1.0** (published 2022-06-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-typed-events
pnpm add simple-typed-events
yarn add simple-typed-events
bun add simple-typed-events
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-06-18 |
| First published | 2021-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 21.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | bsssshhhhhhh |
| Keywords | event emitter, event-emitter, events, type-safe event, typescript event |

## Links

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

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

- 1.1.0 (latest) — 2022-06-18
- 1.0.11 — 2022-06-18
- 1.0.10 — 2021-10-20
- 1.0.9 — 2021-10-20
- 1.0.8 — 2021-10-07
- 1.0.7 — 2021-09-28
- 1.0.6 — 2021-09-25
- 1.0.5 — 2021-09-25
- 1.0.4 — 2021-09-25
- 1.0.2 — 2021-09-25
- 1.0.1 — 2021-09-25
- 1.0.0 — 2021-09-25

## README

# simple-typed-events

[![npm](https://img.shields.io/npm/v/simple-typed-events)](https://www.npmjs.com/package/simple-typed-events) [![codecov](https://codecov.io/gh/bsssshhhhhhh/simple-typed-events/branch/master/graph/badge.svg?token=SZMB2OHTTH)](https://codecov.io/gh/bsssshhhhhhh/simple-typed-events)

Simple event emitter interface with full TypeScript support. Simply define your event names and their callbacks in an interface and pass it to `createEventEmitter` as a generic arg.

---

## Function list:

- `createEventEmitter<T>()` - returns an object with 4 functions
  - `on` - Attach event listener
  - `off` - Detach event listener
  - `once` - Attach an event listener that will only fire once
  - `emit` - Emit an event

```ts
import { createEventEmitter } from 'simple-typed-events';

interface Events {
  start: () => void;
  progress: (loaded: number, total: number) => void;
  finish: () => void;
}

const emitter = createEventEmitter<Events>();

// attach event listener with on()
emitter.on('start', () => {});

// the Events interface can be used as a lookup type
const onProgress: Events['progress'] = (loaded, total) => {
  // ...
};

emitter.on('progress', onProgress);
emitter.on('progress', (loaded, total) => {
  // ...
});

emitter.once('finish', () => {
  // ...
});

emitter.emit('progress'); // Typescript error! the args of the progress event must be passed
emitter.emit('progress', 3); // Still not legal. Both args must be passed
emitter.emit('progress', 3, 5); // no error

// detach listeners by reference
emitter.off('progress', onProgress);
```

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