-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
types(Client): EventEmitter
static method overrides
#10360
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Skipped Deployments
|
08d30f9
to
8dd69cf
Compare
This is a breaking change, as before the point was to also support EventEmitter.on/once returning strictly typed results. Can't we use namespace merging maybe to add overloads to those functions? |
Namespace function declarations and class static method declarations aren't merge-compatible, so this isn't an option. (example) It's possible to use namespace declarations to add method-like function properties to the class constructor type, but not to add overloads to static methods that already exist. The question has been asked before (microsoft/TypeScript#39870) but there's just no valid way to do it in TS as things stand. We got away with just redefining the class due to the symbol mergeability bug in the TypeScript compiler, but now that's been patched, the only way to fix the resulting build errors is to remove the This patch isn't truly "breaking", in the sense that any usage of |
by with declare module "node:events" in discord.js see: discordjs/discord.js#10360
More reports of breaking are coming in as people are attempting to upgrade to TS 5.5, so I'm going to mark this as ready for review. As a minimum changeset to unbreak TS 5.5 builds, the As for how best to provide the type-safe DJS versions of on/once, I think that adding overloads for the inherited static methods |
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 think this is a good solution. Sucks that it has to be removed but I don’t see any other ways around it
The more appropriate issue seems to be microsoft/TypeScript#2957 which is still open and might see some more attention now that the class merging "bug" has been "fixed". The other thing worthy of note is DefinitelyTyped/DefinitelyTyped#68313 which added strict typing to I'm suggesting we block this until we get an answer on the static members getting generics. The only concern I have is the potential incompatibility between our |
@ckohen: Generics would be ideal, but the question is whether or not it's a good idea to leave the repo in a build-breaking state until such a time as that feature gets added to DT and then makes it into a @types/node package build. Even if only a temporising measure, having static method definitions on The TS syntax proposal is nearly a decade old, so while re-opening the discussion is a good idea, I wouldn't be holding out for it. |
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.
The new code is fine, however, we want to wait for DefinitelyTyped/DefinitelyTyped#69997 first so it's correctly updated.
I don't think the DT team is looking to merge DefinitelyTyped/DefinitelyTyped#69997, at least for now. Could this PR get merged since it fixes usage with TS 5.5+? |
DT will not merge it as is but refuses to respond to say how it can be merged, so it's on them. They broke it in the first place and are unwilling to fix it. Regardless, no matter how we do this it's a breaking change, so we're heavily considering fix-breaking this in a more sustainable way. |
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Please describe the changes this PR makes and why it should be merged:
Fixes a build failure in TypeScript 5.5.
Resolves #10358.
See discussion at the associated issue.
Currently, the static
on()
andonce()
methods on Node'sEventEmitter
are overloaded in the discord.js typings to allow for type-checked event handling.TypeScript 5.5.1-rc fixed the bug that allowed class merging here, and this approach is no longer possible. The
EventEmitter
class declaration in typings.d.ts causes breaking errors fornode:events
module imports in TS >=5.5.1, and needs to be removed.It would still be useful for these methods to have access to this type-safe behaviour for client events, but we can't add static method overloads to
EventEmitter
itself.Instead, we can override those methods further down the inheritance chain, and provide
Client.on()
andClient.once()
as the type-safe methods for those who wish to use them. In other words:This won't break any existing builds that use
EventEmitter.on()
orEventEmitter.once()
, as any resolved event argument type will now just becomeany
. However, in order to revert back to the type-safe behaviour, they'd need to use the static methods onClient
rather than onEventEmitter
.This patch also adds the optional
options
parameter (Node >=14) to the method overrides.Status and versioning classification: