Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fully type-checked NodeJS EventEmitter

License

NotificationsYou must be signed in to change notification settings

binier/tiny-typed-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Have your events and their listeners type-checked withno overhead.

npm version

Install

Simply add the dependency usingnpm:

$npm i tiny-typed-emitter

or usingyarn:

$yarn add tiny-typed-emitter

Usage

  1. importtiny-typed-emitter library:
import{TypedEmitter}from'tiny-typed-emitter';
  1. define events and their listener signatures (note: quotes around event names are not mandatory):
interfaceMyClassEvents{'added':(el:string,wasNew:boolean)=>void;'deleted':(deletedCount:number)=>void;}
  1. on this step depending on your use case, you can:
  • define your custom class extendingEventEmitter:
    classMyClassextendsTypedEmitter<MyClassEvents>{constructor(){super();}}
  • create new event emitter instance:
    constemitter=newTypedEmitter<MyClassEvent>();

Generic events interface

To use with generic events interface:

interfaceMyClassEvents<T>{'added':(el:T,wasNew:boolean)=>void;}classMyClass<T>extendsTypedEmitter<MyClassEvents<T>>{}

Compatible subclasses with different events

The type ofeventNames() is a superset of the actual event names to makesubclasses of aTypedEmitter that introduce different events typecompatible. For example the following is possible:

classAnimal<EextendsListenerSignature<E>=ListenerSignature<unknown>>extendsTypedEmitter<{spawn:()=>void}&E>{constructor(){super();}}classFrog<EextendsListenerSignature<E>>extendsAnimal<{jump:()=>void}&E>{}classBird<EextendsListenerSignature<E>>extendsAnimal<{fly:()=>void}&E>{}constanimals:Animal[]=[newFrog(),newBird()];

No Overhead

Library adds no overhead. All it does is it simply reexports renamedEventEmitterwith customized typings.You can checklib/index.js to see the exported code.


[8]ページ先頭

©2009-2025 Movatter.jp