discord.js
discord.js copied to clipboard
types(Client): `EventEmitter` static method overrides
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()
and once()
methods on Node's EventEmitter
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 for node: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()
and Client.once()
as the type-safe methods for those who wish to use them. In other words:
// old
EventEmitter.on(client, 'messageCreate'); // AsyncIterableIterator<[message: Message<boolean>]>
Client.on(client, 'messageCreate'); // AsyncIterableIterator<[message: Message<boolean>]>
// new
EventEmitter.on(client, 'messageCreate'); // AsyncIterableIterator<any[]>
Client.on(client, 'messageCreate'); // AsyncIterableIterator<[message: Message<boolean>]>
// old
EventEmitter.once(client, 'interactionCreate'); // Promise<[interaction: Interaction<CacheType>]>
Client.once(client, 'interactionCreate'); // Promise<[interaction: Interaction<CacheType>]>
// new
EventEmitter.once(client, 'interactionCreate'); // Promise<any[]>
Client.once(client, 'interactionCreate'); // Promise<[interaction: Interaction<CacheType>]>
This won't break any existing builds that use EventEmitter.on()
or EventEmitter.once()
, as any resolved event argument type will now just become any
. However, in order to revert back to the type-safe behaviour, they'd need to use the static methods on Client
rather than on EventEmitter
.
This patch also adds the optional options
parameter (Node >=14) to the method overrides.
Status and versioning classification:
- Code changes have been tested against the Discord API, or there are no code changes
- I know how to update typings and have done so, or typings don't need updating