https://github.com/jeppech/typed-emitter
Typed EventEmitter
https://github.com/jeppech/typed-emitter
Last synced: 1 day ago
JSON representation
Typed EventEmitter
- Host: GitHub
- URL: https://github.com/jeppech/typed-emitter
- Owner: jeppech
- Created: 2021-01-22T12:30:05.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-25T12:03:56.000Z (over 2 years ago)
- Last Synced: 2025-11-13T06:03:52.681Z (7 months ago)
- Language: TypeScript
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Typed event emitter
This is a fairly simple class, for emitting/subscriping to typed events
## Installation
Use your favourite package manager.
```
pnpm add @jeppech/typed-emitter
```
## Usage
```ts
import { TypedEmitter } from '@jeppech/typed-emitter'
// Start by declaring an interface, describing your events and data types
interface EventData {
id: number
user: string
message: string
}
interface MyEvents {
'data': EventData
'connected': undefined
'disconnected': undefined
}
const emitter = TypedEmitter()
// Note you get hints, when using the methods of the emitter.
emitter.on('data', (data) => {
// data is EventData-type
})
emitter.emit('data', {
'id': 1,
'user': 'jeppech',
'message': 'Hello!'
})
```
## Extending class
```ts
class MyClass extends TypedEmitter {
constructor() {
super()
this.on('data', (data) => {
// data is EventData-type
})
}
}
```