tiny-typed-emitter icon indicating copy to clipboard operation
tiny-typed-emitter copied to clipboard

Issue with subclasses

Open Valandur opened this issue 4 years ago • 2 comments

Hi, thanks for the great library :+1:

I have a question about the subclasses with different events example that you provided in the readme:

The example works when calling the events from outside the class, but not from within the class itself. Specifically, if I alter the example to be as follows:

class Animal<E extends ListenerSignature<E> = ListenerSignature<unknown>> extends TypedEmitter<{ spawn: () => void } & E> {
	public constructor() {
		super();
	}

	private doSpawn() {
		this.emit('spawn');
	}
}

class Frog<E extends ListenerSignature<E>> extends Animal<{ jump: (testing: boolean) => void } & E> {}
class Bird<E extends ListenerSignature<E>> extends Animal<{ fly: () => void } & E> {}

Then the compiler fails at the this.emit('spawn'); line with the error: Argument of type '[]' is not assignable to parameter of type 'Parameters<({ spawn: () => void; } & E)["spawn"]>'.

I'm assuming this is because of some constraint that can't be applied, but I can't seem to wrap my head around it. Is there any way to solve this?

Valandur avatar Feb 16 '21 13:02 Valandur

I finally worked around this issue by following how FeathersJS extends its ServiceTypes interface; I failed to realize that we can extend interfaces from different files using the module syntax, declare module './declaration-ts-file', within the file of the class you want to extend events.

To make this work, follow these steps:

  1. Create a TS file to store the base events (i.e. ServiceEvents.ts)
  2. In that base events file add your events interface (i.e. export interface ServiceEvents { someEvent: () => void; })
  3. In your base class file, extends TypedEmitter<ServiceEvents> as you normally would
  4. In your child class file, extend the base class
  5. In that same child class file, declare a module with the filename of the base events interface as follows...

declare module './ServiceEvents' { interface ServiceEvents { newEvent: (data: string) => void; } }

You will now have a strongly typed "newEvent" for the child class and observe that other child classes will not have that new event, as expected.

Notice that I created the interfaces and classes in separate files as to eliminate any possibility for a collision with another declare module './ServiceEvents' declaration.

Enjoy.

scottpageindysoft avatar Mar 20 '21 02:03 scottpageindysoft

Scratch my previous. I tested it in my module, published the module, and everything worked okay with 2 files. Beyond that, it spews the events into other types. It was probably doing that with 2 files, but I didn't catch it.

So, you could use what I stated previously, however you will end up with code bleed all over.

I have another solution that I've been using for a few months, but it's hackish. I'll post it here when I get a minute. Basically you'll need to create your own on and once overrides for the emitter with new generic type defs. From what I recall I also had to wrap another class that extended EventEmitter.

scottpageindysoft avatar Mar 20 '21 04:03 scottpageindysoft