Gathering detailed insights and metrics for @dannyfranca/strict-event-emitter-types
Gathering detailed insights and metrics for @dannyfranca/strict-event-emitter-types
Gathering detailed insights and metrics for @dannyfranca/strict-event-emitter-types
Gathering detailed insights and metrics for @dannyfranca/strict-event-emitter-types
A type-only library for strongly typing any event emitter
npm install @dannyfranca/strict-event-emitter-types
Typescript
Module System
Node Version
NPM Version
67
Supply Chain
99.2
Quality
75.2
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
461
Last Day
2
Last Week
2
Last Month
4
Last Year
49
273 Stars
35 Commits
12 Forks
10 Watching
1 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
2.0.1
Package Id
@dannyfranca/strict-event-emitter-types@2.0.1
Unpacked Size
11.17 kB
Size
3.21 kB
File Count
6
NPM Version
6.13.4
Node Version
13.6.0
Cumulative downloads
Total Downloads
Last day
100%
2
Compared to previous day
Last week
100%
2
Compared to previous week
Last month
100%
4
Compared to previous month
Last year
-46.7%
49
Compared to previous year
2
NOTE: REQUIRES TYPESCRIPT 3.0
A TypeScript library for strongly typed event emitters in 0 kB. Declare events using a simple interface mapping event names to their payloads to get stricter versions of emit
, on
, and other common EventEmitter APIs. Works with any kind of EventEmitter.
npm i strict-event-emitter-types
1import StrictEventEmitter from 'strict-event-emitter-types'; 2 3// define your events 4interface Events { 5 request: (request: Request, response: Response) => void; 6 done: void; 7} 8 9// grab an event emitter 10import { EventEmitter } from 'events'; 11 12// Create strict event emitter types 13let ee: StrictEventEmitter<EventEmitter, Events> = new EventEmitter; 14 15// now enjoy your strongly typed EventEmitter API!🎉 16 17ee.on('request', (r, resp) => ... ); 18// r and resp are contextually typed to Request and Response 19 20ee.on('somethingElse'); 21// Error: unknown event 22 23ee.on('done', x => x); 24// Error: The 'done' event does not have a payload 25 26ee.emit('request', new Request()); 27// Error: missing event payload (the response) 28 29ee.emit('request', new Request(), false); 30// Error: incorrect payload type
Event records are interfaces or object types that map event names to the event's payload types. In the following example, three events are declared:
1interface Events { 2 req: (request: Request, response: Response) => void; 3 done: void; 4 conn: Connection; 5}
Each event shows one of three ways to type the event payloads:
void
: A shortcut for an event with no payload, i.e. () => void
(p: number) => void
can be written as just number
.The default export. A generic type that takes three type parameters:
The third parameter is handy when typing web sockets where client and server can listen to and emit different events. For example, if you are using socket.io:
1// create types representing the server side and client 2// side sockets 3export type ServerSocket = 4 StrictEventEmitter<SocketIO.Socket, EventsFromServer, EventsFromClient>; 5export type ClientSocket = 6 StrictEventEmitter<SocketIOClient.Socket, EventsFromClient, EventsFromServer>; 7 8// elsewhere on server 9let serverSocket: ServerSocket = new SocketIO.Socket(); 10serverSocket.on(/* only events that are sent from the client are allowed */, ...) 11serverSocket.emit(/* only events that are emitted from the server are allowed */, ...) 12 13// elsewhere on client 14let clientSocket: ClientSocket = new SocketIOClient.Socket(); 15clientSocket.on(/* only events that are sent from the server are allowed */, ...) 16clientSocket.emit(/* only events that are emitted from the client are allowed */, ...)
To subclass an EventEmitter you need to cast the base EventEmitter to the strict EventEmitter before extending:
1type MyEmitter = StrictEventEmitter<EventEmitter, Events>; 2 3class MyEventEmitter extends (EventEmitter as { new(): MyEmitter }) { 4 doEmit() { 5 this.emit(...); // strict 6 } 7}
A type for a function which takes (and strictly checks) an emit event and a payload. TStrictEventEmitter is the event emitter type instantiated from StrictEventEmitter.
Useful for broadcast abstractions. It is not possible to contextually type assigments to this type, so your declarations will look something like this:
1import { StrictBroadcast } from 'strict-event-emitter-types'; 2 3const broadcast: StrictBroadcast<ServerSocket> = function( 4 event: string, 5 payload?: any 6) { 7 // ... 8};
Note that the loose types for event and payload only apply inside the broadcast function (consumers will see a much stricter signature). Declaring more precise parameter types or narrowing using type guards would allow strongly-typed dispatching to emitters.
No vulnerabilities found.
No security vulnerabilities found.