Closed alexlafroscia closed 1 year ago
I have tried playing with an EventsFor
type (again, borrowed from #24) that extracts the supported events, in the hopes that that would help illuminate what's going on
export type EventsFor<T extends TypedEmitter<any>> =
T extends TypedEmitter<infer EventMap>
? keyof EventMap
: never;
This works great with variables declared directly as a TypedEmitter<Events>
// knows that only "foo" is a valid option
const event: EventsFor<TypedEmitter<Events>> = "foo";
but again, it breaks down for a class extending EventEmitter
as a TypedEmitter
:
// allows any `string | number`; this should error but does not
const event: EventsFor<MyEmitter> = 42;
Thanks to some help from a co-worker, I was able to get to a version of TypedEventEmitter
that works with my helper function!
Basically, the issue ended up being that TypedEventEmitter
did not have it's own reference to the Events
type, so my on
function had no way to look it up. That could be resolved by adding an optional member to the interface, of the Events
type
interface TypedEventEmitter<Events extends EventMap> {
+ __events_ref?: Events;
}
Leaking this in the API of any TypedEventEmitter
is not ideal, but we can use a declared class instead of an interface to fix that
- interface TypedEventEmitter<Events extends EventMap> {
+ declare class TypedEventEmitter<Events extends EventMap> {
- __events_ref?: Events;
+ private __events_ref?: Events;
}
Without any other changes, my on
helper can now extract the right event-type information from my classes that extend from EventEmitter
as a TypedEventEmitter
!
I'm going to close this issue, since my problem is resolved; I don't know if that's a change you'd want to make to this library, but hopefully this helps resolve an issue for someone down the road!
I'm running into a problem with trying to correctly type a utility function when using the "base class" version of
TypedEmitter
.My application has an
on
helper that wrapsEventEmitter#on
but returns a function that callsEventEmitter#off
for you:This works great under certain circumstances; namely, variables that are directly declared to be of some
TypedEmitter<Events>
typeHowever, in many parts of my codebase, I'm using the ability to extend a class from
EventEmitter
as aTypedEmitter
, which breaks myon
helperI think this is related, conceptually, to #24 but I was unable to use the examples in there to alter my
on
helper so that it would respect the types supported by the class.Do you have any ideas on how to type a helper function so that it is able to "see the valid events" when using the base-class approach?