Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

strict-event-emitter-types

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strict-event-emitter-types

Strictly and safely type any EventEmitter-like interface on any object

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
162K
decreased by-8.09%
Maintainers
1
Weekly downloads
 
Created

What is strict-event-emitter-types?

The 'strict-event-emitter-types' npm package provides TypeScript types for strongly-typed event emitters. It allows developers to define event types and their corresponding payloads, ensuring type safety when emitting and listening to events.

What are strict-event-emitter-types's main functionalities?

Defining Typed Event Emitters

This feature allows you to define a typed event emitter with specific event names and their corresponding payload types. The code sample demonstrates how to create a custom event emitter class with typed events 'foo' and 'bar'.

const { EventEmitter } = require('events');
const { StrictEventEmitter } = require('strict-event-emitter-types');

interface Events {
  foo: (arg: string) => void;
  bar: (arg: number) => void;
}

class MyEmitter extends (EventEmitter as new () => StrictEventEmitter<EventEmitter, Events>) {}

const emitter = new MyEmitter();
emitter.on('foo', (arg) => console.log(arg));
emitter.emit('foo', 'Hello, world!');

Ensuring Type Safety

This feature ensures type safety by enforcing the correct argument types for each event. The code sample shows how TypeScript will throw an error if the argument type does not match the expected type for the event.

const { EventEmitter } = require('events');
const { StrictEventEmitter } = require('strict-event-emitter-types');

interface Events {
  foo: (arg: string) => void;
  bar: (arg: number) => void;
}

class MyEmitter extends (EventEmitter as new () => StrictEventEmitter<EventEmitter, Events>) {}

const emitter = new MyEmitter();

// TypeScript will throw an error if the argument type does not match
// emitter.emit('foo', 123); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
emitter.emit('foo', 'Hello, world!');

Typed Event Listeners

This feature allows you to define typed event listeners, ensuring that the callback functions match the expected argument types for each event. The code sample demonstrates how TypeScript will throw an error if the listener callback does not match the expected type.

const { EventEmitter } = require('events');
const { StrictEventEmitter } = require('strict-event-emitter-types');

interface Events {
  foo: (arg: string) => void;
  bar: (arg: number) => void;
}

class MyEmitter extends (EventEmitter as new () => StrictEventEmitter<EventEmitter, Events>) {}

const emitter = new MyEmitter();

// TypeScript will throw an error if the listener callback does not match the expected type
// emitter.on('foo', (arg: number) => console.log(arg)); // Error: Argument of type '(arg: number) => void' is not assignable to parameter of type '(arg: string) => void'.
emitter.on('foo', (arg: string) => console.log(arg));

Other packages similar to strict-event-emitter-types

Keywords

FAQs

Package last updated on 03 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc