Open StarManTheGamer opened 3 weeks ago
Upon more investigating, by removing this class from the script it resolved the issues. This is why I believe this is an issue with discord.js
A quick inspection leads to this being reproducible only with TypeScript 5.5.2.
Another quick inspection shows that this is not related to the @types/node
update. The problem persists, even going back as far as @types/node@20.14.7
, which was released 2 months ago.
However, typescript@5.5.2
was released today, so that lines up much better with the time frame.
I also encountered this issue when type-checking my personal project that uses discord.js
after updating to typescript@5.5.2
.
After further investigation, I found that TS 5.5 throws errors for module augmentation, which is what you are doing to the EventEmitter
class in typings/index.d.ts
. I was able to reproduce the problem outside of discord.js
by trying to augment EventEmitter
myself. I'm still investigating whether it is specific to @types/node
or affects all augmentation in general.
I can use skipLibCheck
in my personal project as a workaround, but that will not work for building discord.js
itself, should you choose to update to TS 5.5.
I don't see a bug report in the TypeScript repo yet, so I've been doing more researching and thinking about submitting one.
I found https://github.com/microsoft/TypeScript/issues/58907 earlier, but I was not sure if it was related.
Yeah it's happening outside of discord.js, I can't seem to myclass extends EventEmitter
anywhere in TypeScript 5.5 unless I explicitly define the .on
in my interface like:
public on<Event extends keyof ClientEvents>(event: Event, listener: (...args: ClientEvents[Event]) => void): this;
This sounds more like a TS 5.5.2 issue, which I'd recommend opening an issue on their side (if one wasn't opened already)
Classes are explicitly unmergeable; this only worked previously due to a specific bug related to merging aliased symbols, which was patched in microsoft/TypeScript#58326. The error here is unfortunately intended behaviour.
Methods on the class prototype are overloadable via interface merging, but as things stand, static methods on the constructor aren't.
Classes are explicitly unmergeable; this only worked previously due to a specific bug related to merging aliased symbols, which was patched in microsoft/TypeScript#58326. The error here is unfortunately intended behaviour.
The class prototype is extensible via interface merging, but as things stand, the constructor isn't. Unfortunately, this means no ability to add or extend class static methods.
So is there a better solution to prevent the errors?
On our side, we could use interface merging, but that has its own can of worms. We probably want to take a look at adding these extensions only on the client(s)
There is an alternative approach, I suppose. Client.on()
and Client.once()
are currently the same as EventEmitter.on()
and EventEmitter.once()
via inheritance, but maybe those could be overridden with the type-safe signatures:
export class Client<Ready extends boolean = boolean> extends BaseClient {
// ...
// Override static EventEmitter methods, with added type checks for Client events.
public static once<Emitter extends EventEmitter, Event extends keyof ClientEvents>(
eventEmitter: Emitter,
eventName: Emitter extends Client ? Event : string | symbol,
): Promise<Emitter extends Client ? ClientEvents[Event] : any[]>;
public static on<Emitter extends EventEmitter, Events extends keyof ClientEvents>(
eventEmitter: Emitter,
eventName: Emitter extends Client ? Events : string | symbol,
): AsyncIterableIterator<Emitter extends Client ? ClientEvents[Events] : any[]>;
}
This would provide type-safe versions of those methods, without needing to interfere with EventEmitter
.
(vlad: I think this is probably the sort of thing you were alluding to in your comment?)
See #7986 for why this augmentation was added in the first place - Client#on()
should work the same even without it.
as a temporary workaround:
npm i -D typescript@5.4.5
yarn add typescript@5.4.5 --dev
prevents EventEmitter already defined errors in the project
For future reference:
A bug report was submitted in the TypeScript repo, and the maintainers confirmed that this is intended behavior - class augmentations cannot be merged.
Any update on this issue? I was hoping to have a fix soon so I could update my typescript.
Any update on this issue? I was hoping to have a fix soon so I could update my typescript.
They made a pr which solves the issue, still waiting for merge, but you can install the commit manually by doing: https://github.com/discordjs/discord.js/pull/10360
to install the pr correctly, first: uninstall discord.js
npm uninstall discord.js
second: add the following to your package.json (to still use it as "discord.js"
"resolutions": {
"discord.js": "github:discordjs/discord.js#8dd69cf2811d86ed8da2a7b1e9a6a94022554e96"
},
third: then install it by doing
npm i discordjs/discord.js#8dd69cf2811d86ed8da2a7b1e9a6a94022554e96
sometimes you have to call npm i
again to make sure your resolutions work
you can also do the following instead of installing the fix of pr, while beeing on the latest ts version
class yourCustomEventEmitterClass extends EventEmitter implements NodeJS.EventEmitter {
}
this will work fine for types
I‘d still suggest not doing too many manual workarounds like that. Instead wait for a proper fix, which will probably be in @types/node
to support fully typed EventEmitter. Then discord.js
can just drop the module augmentation altogether and it will „just work“™️.
When that happens the linked PR would be obsolete too.
I don't believe using a resolution like that would work since this is a monorepo...
Another workaround is to use https://yarnpkg.com/package?name=eventemitter2 which is compatible with node's EventEmitter like code
Which package is this bug report for?
discord.js
Issue description
Hi there, approximately two hours ago @nodes/types upgraded their package. I use GitHub Actions to build and test my discord bot, and I didn't realize I was out-of-date until I got conflicting errors that were very confusing.
As far as I can tell, I believe that @ nodes/types did some important changes and now discord.js is conflicting with those changes, and this is an issue with discord.js but I'm not sure. I'm not overly a huge expert on these types of things.
You can replicate this issue by installing the latest @nodes/types and the latest development build of discord.js. Upon attempting to build your project, you will be greeted with this error:
Code sample
Versions
typescript - 5.5.2 discord-js - latest development build @types/node - 20.14.7
Node - Latest v20
Issue priority
High (immediate attention needed)
Which partials do you have configured?
No Partials
Which gateway intents are you subscribing to?
Guilds, GuildMembers, GuildMessages, MessageContent
I have tested this issue on a development release
No response