JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 169
  • Score
    100M100P100Q88759F
  • License MIT

Typed Event Emitter is a TypeScript library that provides a strongly typed event emitter, allowing developers to define custom events with specific payloads, ensuring type safety and reducing runtime errors in event-driven applications.

Package Exports

  • @nowarajs/typed-event-emitter
  • @nowarajs/typed-event-emitter/types

Readme

๐ŸŽฏ NowaraJS - TypedEventEmitter

๐Ÿ“Œ Table of Contents

๐Ÿ“ Description

A TypeScript library that provides a strongly typed event emitter for type-safe event handling.

TypedEventEmitter extends EventEmitter with full TypeScript support, allowing developers to define custom events with specific payloads while ensuring complete type safety and reducing runtime errors in event-driven applications.

โœจ Features

  • ๐Ÿ”’ Type Safety: Full TypeScript support with strongly typed event names and payloads
  • ๐Ÿงฉ Generic Design: Define custom event maps for your specific use cases
  • ๐Ÿ”„ Familiar API: Extends EventEmitter with the same familiar methods
  • ๐Ÿ“ฆ Zero Dependencies: No external runtime dependencies
  • ๐Ÿš€ Modern: Built with modern TypeScript and supports ESM
  • ๐Ÿงช Well Tested: Comprehensive test suite included

๐Ÿ”ง Installation

bun add @nowarajs/typed-event-emitter

โš™๏ธ Usage

Basic Usage

import { TypedEventEmitter } from '@nowarajs/typed-event-emitter';

// Define your event map
interface MyEvents {
    userLogin: [{ userId: string; timestamp: Date }];
    userLogout: [{ userId: string }];
    dataUpdate: [{ id: number; data: any }];
    error: [Error];
}

// Create a typed event emitter
const emitter = new TypedEventEmitter<MyEvents>();

// Type-safe event listening
emitter.on('userLogin', (payload) => {
    // payload is automatically typed as { userId: string; timestamp: Date }
    console.log(`User ${payload.userId} logged in at ${payload.timestamp}`);
});

// Type-safe event emission
emitter.emit('userLogin', { 
    userId: 'user123', 
    timestamp: new Date() 
});

Advanced Usage

import { TypedEventEmitter } from '@nowarajs/typed-event-emitter';

// Multiple parameters
interface Events {
    move: [x: number, y: number];
    click: [button: 'left' | 'right', x: number, y: number];
    keypress: [key: string, modifiers: string[]];
}

const input = new TypedEventEmitter<Events>();

// Multiple parameters are fully typed
input.on('move', (x, y) => {
    console.log(`Mouse moved to ${x}, ${y}`);
});

input.on('click', (button, x, y) => {
    console.log(`${button} click at ${x}, ${y}`);
});

// Emit with multiple arguments
input.emit('move', 100, 200);
input.emit('click', 'left', 50, 75);

๐Ÿ“š API Reference

You can find the complete API reference documentation for TypedEventEmitter at:

โš–๏ธ License

Distributed under the MIT License. See LICENSE for more information.

๐Ÿ“ง Contact