📣 A minimal and strongly typed event emitter with no dependencies — just ~500 bytes gzipped!
This library reuses and extracts parts of the core implementation from
mini-i18n. The original logic was refined and optimized for a standalone event emitter.
- ✅ Type-safe event definitions
- 📦 Zero dependencies and ultra-lightweight
- 🧠 Simple API:
on,off,once,emit,clear - 🧩 Can be used in both browser and Node.js environments
- ⚙️ Ideal for internal module-level event handling
Using npm:
npm install mini-emitUsing CDN:
<script src="https://cdn.jsdelivr.net/npm/mini-emit"></script>
<!-- or -->
<script src="https://unpkg.com/mini-emit"></script>
<script>
const emitter = new miniemit.EventEmitter()
</script>import { EventEmitter } from 'mini-emit'
interface Events {
ready: void
message: string
data: { id: number, name: string }
}
const emitter = new EventEmitter<Events>()
// Subscribe to events
const stop = emitter.on('message', (msg) => {
console.log('Got message:', msg)
})
// Emit events
emitter.emit('ready')
emitter.emit('message', 'Hello world')
emitter.emit('data', { id: 1, name: 'Lete' })
// One-time listener
emitter.once('ready', () => {
console.log('Only once!')
})
// Remove listener
stop()
// Clear all listeners
emitter.clear()
// Clear specific event listeners
emitter.clear('message')Create a new event emitter instance with optional event type map.
Add a listener to the specified event.
const off = emitter.on('eventName', (payload) => {})
off() // Cancel the listenerListen to an event only once.
emitter.once('eventName', (payload) => {
console.log('Triggered only once')
})Remove a specific listener.
emitter.off('eventName', listener)Emit an event with optional payload.
emitter.emit('eventName', data)Clear all listeners or only listeners for a specific event.
emitter.clear() // Clear all
emitter.clear('eventName') // Clear specificWith TypeScript, you can provide an EventMap type to get full type checking on event names and payloads.
interface EventMap {
login: { userId: string }
logout: void
}
const emitter = new EventEmitter<EventMap>()
emitter.emit('login', { userId: 'abc123' }) // ✅ OK
emitter.emit('logout') // ✅ OK
emitter.emit('logout', 'unexpected') // ❌ Type error