Skip to content

Commit

Permalink
types(Client): EventEmitter static method overrides (#10360)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
Renegade334 and kodiakhq[bot] authored Aug 20, 2024
1 parent 5d92525 commit 9b707f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
26 changes: 12 additions & 14 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,6 @@ import {
RawWidgetMemberData,
} from './rawDataTypes.js';

declare module 'node:events' {
class EventEmitter {
// Add type overloads for client events.
public static once<Emitter extends EventEmitter, Event extends keyof ClientEvents>(
eventEmitter: Emitter,
eventName: Emitter extends Client ? Event : string,
): 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,
): AsyncIterableIterator<Emitter extends Client ? ClientEvents[Events] : any>;
}
}

//#region Classes

export class Activity {
Expand Down Expand Up @@ -999,6 +985,18 @@ export class Client<Ready extends boolean = boolean> 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<Emitter extends EventEmitter, Event extends keyof ClientEvents>(
eventEmitter: Emitter,
eventName: Emitter extends Client ? Event : string | symbol,
options?: { signal?: AbortSignal | undefined },
): Promise<Emitter extends Client ? ClientEvents[Event] : any[]>;
public static on<Emitter extends EventEmitter, Event extends keyof ClientEvents>(
eventEmitter: Emitter,
eventName: Emitter extends Client ? Event : string | symbol,
options?: { signal?: AbortSignal | undefined },
): AsyncIterableIterator<Emitter extends Client ? ClientEvents[Event] : any[]>;

public application: If<Ready, ClientApplication>;
public channels: ChannelManager;
public get emojis(): BaseGuildEmojiManager;
Expand Down
4 changes: 4 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,10 @@ client.on('guildCreate', async g => {
);
});

// EventEmitter static method overrides
expectType<Promise<[Client<true>]>>(Client.once(client, 'ready'));
expectType<AsyncIterableIterator<[Client<true>]>>(Client.on(client, 'ready'));

client.login('absolutely-valid-token');

declare const loggedInClient: Client<true>;
Expand Down

0 comments on commit 9b707f2

Please sign in to comment.