Gathering detailed insights and metrics for @kim-kijun/typed-event-emitter
Gathering detailed insights and metrics for @kim-kijun/typed-event-emitter
npm install @kim-kijun/typed-event-emitter
Typescript
Module System
66.4
Supply Chain
99.2
Quality
75.2
Maintenance
100
Vulnerability
100
License
TypeScript (94.95%)
JavaScript (5.05%)
Total Downloads
482
Last Day
1
Last Week
1
Last Month
2
Last Year
77
4 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.2
Package Id
@kim-kijun/typed-event-emitter@1.0.2
Unpacked Size
19.78 kB
Size
7.07 kB
File Count
9
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
1
Compared to previous week
Last month
0%
2
Compared to previous month
Last year
-47.6%
77
Compared to previous year
1
You can use node module event-emitter with strict type in typescript.
npm i @kim-kijun/typed-event-emitter
TypedEventEmitter
You can use TypedEventEmitter
like EventEmitter. (It's same.)
1import { TypedEventEmitter } from '@kim-kijun/typed-event-emitter'; 2 3// Predefine event types 4type MyEvents = { 5 loadedEnd(): void; 6 changeValue(value: string): void; 7 pressKey(key: string, keycode: number, ctrlKey?: boolean): void; 8}; 9 10// Call constructor with the defined type. 11const eventEmitter = new TypedEventEmitter<MyEvents>();
1eventEmitter.on('loadidEnd', () => {}); // Type Error! - wrong event name 2eventEmitter.on('loadedEnd', () => {}); // Good!
1eventEmitter.on('changeValue', (value) => { 2 let fixed = value.toFixed(2); // Type Error! - 'value' should be used as string type 3}); 4eventEmitter.on('changeValue', (value) => { 5 let threeStr = value.slice(0, 3); // Good! 6});
1eventEmitter.emit('pressKey', 'a', '80', true); // Type Error! - second argument should be number. 2eventEmitter.emit('pressKey', 'a', 0, true); // Good!
1class MyEventEmitter extends TypedEventEmitter<MyEvents> {...} 2const myEventEmitter = new MyEventEmitter(); 3myEventEmitter.on('loadidEnd', () => {}); // Type Error! 4myEventEmitter.on('loadedEnd', () => {}); // Good!
1type MyEvents = { 2 loadedEnd(): void; 3 changeValue(value: string): void; 4 pressKey(key: string, keycode: number, ctrlKey?: boolean): void; 5}; 6const eventEmitter = new TypedEventEmitter<MyEvents>();
EventName
1import { EventName } from '@kim-kijun/typed-event-emitter'; 2type ExtractedEvents = EventName<MyEvents>; 3// 'loadedEnd' | 'changeValue' | 'pressKey'
EventHandler
1import { EventHandler } from '@kim-kijun/typed-event-emitter'; 2type HandlerChangeValue = EventHandler<MyEvents, 'changeValue'>; 3// (value: string): void
EventHandlerParams
1import { EventHandlerParams } from '@kim-kijun/typed-event-emitter'; 2type HandlerParamsPressKey = EventHandlerParams<MyEvents, 'pressKey'>; 3// [string, number, boolean | undefined]
ExtractEventType
1import { ExtractEventType } from '@kim-kijun/typed-event-emitter'; 2type ExtractedEvents = ExtractEventType<typeof eventEmitter>; 3// equals to 'MyEvents'
No vulnerabilities found.
No security vulnerabilities found.