From 9b707f2b832a57d5768757fad09cf8982f64d03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= <9092381+Renegade334@users.noreply.github.com> Date: Tue, 20 Aug 2024 10:52:36 +0100 Subject: [PATCH] types(Client): `EventEmitter` static method overrides (#10360) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/discord.js/typings/index.d.ts | 26 ++++++++++----------- packages/discord.js/typings/index.test-d.ts | 4 ++++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 2acb6c38df15..7a7231c497b3 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -244,20 +244,6 @@ import { RawWidgetMemberData, } from './rawDataTypes.js'; -declare module 'node:events' { - class EventEmitter { - // Add type overloads for client events. - public static once( - eventEmitter: Emitter, - eventName: Emitter extends Client ? Event : string, - ): Promise; - public static on( - eventEmitter: Emitter, - eventName: Emitter extends Client ? Events : string, - ): AsyncIterableIterator; - } -} - //#region Classes export class Activity { @@ -999,6 +985,18 @@ export class Client extends BaseClient { // This a technique used to brand the ready state. Or else we'll get `never` errors on typeguard checks. private readonly _ready: Ready; + // Override inherited static EventEmitter methods, with added type checks for Client events. + public static once( + eventEmitter: Emitter, + eventName: Emitter extends Client ? Event : string | symbol, + options?: { signal?: AbortSignal | undefined }, + ): Promise; + public static on( + eventEmitter: Emitter, + eventName: Emitter extends Client ? Event : string | symbol, + options?: { signal?: AbortSignal | undefined }, + ): AsyncIterableIterator; + public application: If; public channels: ChannelManager; public get emojis(): BaseGuildEmojiManager; diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index a3b0e7be102f..f2db5f9a6f58 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -1297,6 +1297,10 @@ client.on('guildCreate', async g => { ); }); +// EventEmitter static method overrides +expectType]>>(Client.once(client, 'ready')); +expectType]>>(Client.on(client, 'ready')); + client.login('absolutely-valid-token'); declare const loggedInClient: Client;