From 669b17000a48ec7d6b666d4b923da89f04d53ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Rom=C3=A1n?= Date: Sat, 26 Jun 2021 23:56:48 +0200 Subject: [PATCH] fix(Sharding): strict type context and return --- typings/index.d.ts | 43 +++++++++++++++++++++++++++++++------------ typings/index.ts | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index c3083c54daa4..0edb2bb92676 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -137,6 +137,7 @@ type Awaited = T | Promise; declare module 'discord.js' { import BaseCollection from '@discordjs/collection'; + import { DiscordGatewayAdapterCreator, DiscordGatewayAdapterLibraryMethods } from '@discordjs/voice'; import { ChildProcess } from 'child_process'; import { APIActionRowComponent, @@ -152,11 +153,11 @@ declare module 'discord.js' { Snowflake as APISnowflake, ApplicationCommandOptionType as ApplicationCommandOptionTypes, ApplicationCommandPermissionType as ApplicationCommandPermissionTypes, + Snowflake as APISnowflake, } from 'discord-api-types/v8'; import { EventEmitter } from 'events'; import { PathLike } from 'fs'; - import { Readable, Stream, Writable } from 'stream'; - import { DiscordGatewayAdapterCreator, DiscordGatewayAdapterLibraryMethods } from '@discordjs/voice'; + import { Stream } from 'stream'; import * as WebSocket from 'ws'; export const version: string; @@ -1756,13 +1757,16 @@ declare module 'discord.js' { public readonly ids: number[]; public mode: ShardingManagerMode; public parentPort: any | null; - public broadcastEval(fn: (client: Client) => T): Promise; - public broadcastEval(fn: (client: Client) => T, options: { shard: number }): Promise; - public broadcastEval(fn: (client: Client, context: P) => T, options: { context: P }): Promise; + public broadcastEval(fn: (client: Client) => T): Promise[]>; + public broadcastEval(fn: (client: Client) => T, options: { shard: number }): Promise>; public broadcastEval( - fn: (client: Client, context: P) => T, + fn: (client: Client, context: Serialized

) => T, + options: { context: P }, + ): Promise[]>; + public broadcastEval( + fn: (client: Client, context: Serialized

) => T, options: { context: P; shard: number }, - ): Promise; + ): Promise>; public fetchClientValues(prop: string): Promise; public fetchClientValues(prop: string, shard: number): Promise; public respawnAll(options?: MultipleShardRespawnOptions): Promise; @@ -1785,13 +1789,16 @@ declare module 'discord.js' { public totalShards: number | 'auto'; public shardList: number[] | 'auto'; public broadcast(message: any): Promise; - public broadcastEval(fn: (client: Client) => T): Promise; - public broadcastEval(fn: (client: Client) => T, options: { shard: number }): Promise; - public broadcastEval(fn: (client: Client, context: P) => T, options: { context: P }): Promise; + public broadcastEval(fn: (client: Client) => T): Promise[]>; + public broadcastEval(fn: (client: Client) => T, options: { shard: number }): Promise>; + public broadcastEval( + fn: (client: Client, context: Serialized

) => T, + options: { context: P }, + ): Promise[]>; public broadcastEval( - fn: (client: Client, context: P) => T, + fn: (client: Client, context: Serialized

) => T, options: { context: P; shard: number }, - ): Promise; + ): Promise>; public createShard(id: number): Shard; public fetchClientValues(prop: string): Promise; public fetchClientValues(prop: string, shard: number): Promise; @@ -4321,5 +4328,17 @@ declare module 'discord.js' { | 'STAGE_INSTANCE_UPDATE' | 'STAGE_INSTANCE_DELETE'; + type Serialized = T extends symbol | bigint | (() => unknown) + ? never + : T extends number | string | boolean | undefined + ? T + : T extends { toJSON(): infer R } + ? R + : T extends ReadonlyArray + ? Serialized[] + : T extends ReadonlyMap | ReadonlySet + ? {} + : { [K in keyof T]: Serialized }; + //#endregion } diff --git a/typings/index.ts b/typings/index.ts index 788f17adc1be..a3c5f16113e5 100644 --- a/typings/index.ts +++ b/typings/index.ts @@ -1,6 +1,15 @@ /// -import { Client, Intents, Message, MessageAttachment, MessageEmbed } from 'discord.js'; +import { + Client, + Collection, + Intents, + Message, + MessageAttachment, + MessageEmbed, + Permissions, + Serialized, +} from 'discord.js'; const client: Client = new Client({ intents: Intents.NON_PRIVILEGED, @@ -48,3 +57,32 @@ client.on('message', ({ channel }) => { }); client.login('absolutely-valid-token'); + +declare const assertType: (value: T) => asserts value is T; +declare const serialize: (value: T) => Serialized; + +assertType(serialize(undefined)); +assertType(serialize(null)); +assertType(serialize([1, 2, 3])); +assertType<{}>(serialize(new Set([1, 2, 3]))); +assertType<{}>( + serialize( + new Map([ + [1, '2'], + [2, '4'], + ]), + ), +); +assertType(serialize(new Permissions(Permissions.FLAGS.ATTACH_FILES))); +assertType(serialize(new Intents(Intents.FLAGS.GUILDS))); +assertType( + serialize( + new Collection([ + [1, '2'], + [2, '4'], + ]), + ), +); +assertType(serialize(Symbol('a'))); +assertType(serialize(() => {})); +assertType(serialize(BigInt(42)));