-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extracting Types from Base Class #35
Comments
I have tried playing with an export type EventsFor<T extends TypedEmitter<any>> =
T extends TypedEmitter<infer EventMap>
? keyof EventMap
: never; This works great with variables declared directly as a // knows that only "foo" is a valid option
const event: EventsFor<TypedEmitter<Events>> = "foo"; but again, it breaks down for a class extending // 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 Basically, the issue ended up being that interface TypedEventEmitter<Events extends EventMap> {
+ __events_ref?: Events;
} Leaking this in the API of any - interface TypedEventEmitter<Events extends EventMap> {
+ declare class TypedEventEmitter<Events extends EventMap> {
- __events_ref?: Events;
+ private __events_ref?: Events;
} Without any other changes, my |
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?
The text was updated successfully, but these errors were encountered: