Better type classes that extends EventEmitter.
See original GitHub issueHi, I think that you should better type the classes that extend EventEmitter, using Generics to list and strictly type the events available, instead of just having string | symbol
for the name and fn: (...args: Array<any>) => void
for the function.
There are a lot of libraries that provide classes with typed events and the one you are using, eventemitter3
can also do it if you update the version you use.
This is an example of what we have to do without modifying the PIXI to get the proper types :
export type EventList = Record<string, any[]>;
export namespace EventEmitter {
export type Emit<EventTypes extends EventList> = <K extends keyof EventTypes & string>(event: K, ...args: EventTypes[K]) => boolean;
export type On<EventTypes extends EventList, This> = <K extends keyof EventTypes & string>(event: K, fn: (...params: EventTypes[K]) => void, context?: any) => This;
export type Once<EventTypes extends EventList, This> = <K extends keyof EventTypes & string>(event: K, fn: (...params: EventTypes[K]) => void, context?: any) => This;
}
export interface Sprite {
emit: EventEmitter.Emit<SpriteEvents>;
on: EventEmitter.On<SpriteEvents, this>;
once: EventEmitter.Once<SpriteEvents, this>;
}
export type SpriteEvents = {
added: [container: PIXI.Container];
click: [event: PIXI.InteractionEvent];
mousedown: [event: PIXI.InteractionEvent];
mousemove: [event: PIXI.InteractionEvent];
mouseout: [event: PIXI.InteractionEvent];
mouseover: [event: PIXI.InteractionEvent];
mouseup: [event: PIXI.InteractionEvent];
mouseupoutside: [event: PIXI.InteractionEvent];
pointercancel: [event: PIXI.InteractionEvent];
pointerdown: [event: PIXI.InteractionEvent];
pointermove: [event: PIXI.InteractionEvent];
pointerout: [event: PIXI.InteractionEvent];
pointerover: [event: PIXI.InteractionEvent];
pointertap: [event: PIXI.InteractionEvent];
pointerup: [event: PIXI.InteractionEvent];
pointerupoutside: [event: PIXI.InteractionEvent];
removed: [container: PIXI.Container];
removedFrom: [child: PIXI.DisplayObject, container: PIXI.Container, index: number];
rightclick: [event: PIXI.InteractionEvent];
rightdown: [event: PIXI.InteractionEvent];
rightup: [event: PIXI.InteractionEvent];
rightupoutside: [event: PIXI.InteractionEvent];
tap: [event: PIXI.InteractionEvent];
touchcancel: [event: PIXI.InteractionEvent];
touchend: [event: PIXI.InteractionEvent];
touchendoutside: [event: PIXI.InteractionEvent];
touchmove: [event: PIXI.InteractionEvent];
touchstart: [event: PIXI.InteractionEvent];
};
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Better type classes that extends EventEmitter. #7429 - GitHub
Hi, I think that you should better type the classes that extend EventEmitter, using Generics to list and strictly type the events available, ......
Read more >typescript - class extending EventEmitter with type parameter
This class has some standard events but I want to be able to pass a type parameter to this class that contains some...
Read more >Chapter 4. Events: Mastering EventEmitter and beyond
In this chapter you'll learn how to use EventEmitter to make custom classes, and how it's used within Node and open source modules....
Read more >Extending the Event Emitter - YouTube
Get the COMPLETE Course (45% OFF - LIMITED TIME): https://programmingwithmosh.com/ courses /nodeSubscribe for more videos: ...
Read more >How to use Node.js Event Emitter. | JavaScript in Plain English
How we can use Event Emitter? · 1. Import the module · 2. Create a class which extends · 3. Instantiate from this...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Awesome suggestion. We should incorporate this approach for better typings for events.
Very good work so far, would love to see this merged!