- Notifications
You must be signed in to change notification settings - Fork11
Fully type-checked NodeJS EventEmitter
License
binier/tiny-typed-emitter
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Have your events and their listeners type-checked withno overhead.
Simply add the dependency usingnpm:
$npm i tiny-typed-emitteror usingyarn:
$yarn add tiny-typed-emitter- importtiny-typed-emitter library:
import{TypedEmitter}from'tiny-typed-emitter';
- 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;}
- on this step depending on your use case, you can:
- define your custom class extending
EventEmitter:classMyClassextendsTypedEmitter<MyClassEvents>{constructor(){super();}}
- create new event emitter instance:
constemitter=newTypedEmitter<MyClassEvent>();
To use with generic events interface:
interfaceMyClassEvents<T>{'added':(el:T,wasNew:boolean)=>void;}classMyClass<T>extendsTypedEmitter<MyClassEvents<T>>{}
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()];
Library adds no overhead. All it does is it simply reexports renamedEventEmitterwith customized typings.You can checklib/index.js to see the exported code.
About
Fully type-checked NodeJS EventEmitter
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.