-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
Move the strongly-typed Typed
types into the main types
#69
Move the strongly-typed Typed
types into the main types
#69
Conversation
…e defaults I think the extra type is unnecessary, and it's easier to maintain only one of them! Fixes sindresorhus#67
… list of event names to event data for simplicity.
Ok, since we're ok with a breaking change, updated this to:
Thanks for the feedback! |
|
||
/** | ||
The number of listeners for the `eventName` or all events if not specified. | ||
*/ | ||
listenerCount(eventName?: EventNames): number; | ||
listenerCount(eventName?: keyof EventData): number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make a type alias for type EventNames = keyof EventData
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opted not to do that because to create type aliases from operators on the generics within class definitions like this, you have to use more generic types with defaults. To the best of my knowledge, you can't just do:
class Foo<T> {
type U = keyof T;
// ...
}
you have to do
class Foo<T, U = keyof T> {
// ...
}
For types that are meant to be consumed by end users, which Emittery
definitely is, I always find this confusing. The type you are going to use has more type parameters than you are supposed to pass, and without looking at the code or knowing that the code is using this trick, it seems unclear. So, I'd say its better to be verbose in the class body and keep the top level user facing type interface clean, but happy to change if you feel strongly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized I did this already for the DatalessEvents
type so I am being kind of inconsistent, oops. I will move this up in the same way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AH I remember why this wasn't possible now -- fancy typesystem reasons. Because of the strange no-type-aliases-only-derived-generic-type-defaults thing above, the generic type will usually be the keyof EventNames
type, but could be instantiated with a different type if the user passed it in. For that reason, TypeScript can't for sure, always access EventNames[Name]
, because Name
only defaults to keyof EventNames
, but could be something else. So, it doesn't type check, so I can't really do it. Sorry for all the confusion!
Typed
types into the main types
@mmkal Looks good to you now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm - made a comment re tests. Couple of other questions I wouldn't consider blocking but worth asking since this is will be a breaking release:
- should the
.Typed
alias be removed from js too to avoid confusion? - is there a plan to rewrite in typescript and use
tsc
to generate the d.ts file? If doing that might yield other small breaking changes it'd be nice for users if they got bundled.
Yes |
If anyone wanna do the work, sure. But it's not something I plan to work on. |
81bb090
to
e40fc77
Compare
@airhorns Can you do #69 (comment) ? |
Yeah, while adding the extended tests I noticed some other issues, still trying to work through those. |
1de1f29
to
0a793fb
Compare
Ok, I think this is ready for rereview! |
…n the main Emittery class now
0a793fb
to
902196b
Compare
And ensure they deal with events that don't have data just fine!
902196b
to
809522a
Compare
Make sure you run |
I did 48ea6ca to help you enforce the correct style. |
… on with new xo in 48ea6ca
Ok, corrected all my style issues and the latent typescript lint issues, which are unrelated to this PR but resulted from the |
Looks good. Thank you :) |
I think the extra type is unnecessary, and it's easier to maintain only one of them!
Fixes #67