question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better type classes that extends EventEmitter.

See original GitHub issue

Hi, 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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
bigtimebuddycommented, Apr 20, 2021

Awesome suggestion. We should incorporate this approach for better typings for events.

1reaction
fluffy-heinzelmancommented, Oct 20, 2021

Very good work so far, would love to see this merged!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found