mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
fix(Sharding): strict type context and return (#5933)
This commit is contained in:
40
typings/index.d.ts
vendored
40
typings/index.d.ts
vendored
@@ -150,7 +150,7 @@ declare enum WebhookTypes {
|
|||||||
'Channel Follower' = 2,
|
'Channel Follower' = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Awaited<T> = T | Promise<T>;
|
type Awaited<T> = T | PromiseLike<T>;
|
||||||
|
|
||||||
declare module '@discordjs/voice' {
|
declare module '@discordjs/voice' {
|
||||||
import { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v8';
|
import { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v8';
|
||||||
@@ -1793,13 +1793,16 @@ declare module 'discord.js' {
|
|||||||
public readonly ids: number[];
|
public readonly ids: number[];
|
||||||
public mode: ShardingManagerMode;
|
public mode: ShardingManagerMode;
|
||||||
public parentPort: any | null;
|
public parentPort: any | null;
|
||||||
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
|
public broadcastEval<T>(fn: (client: Client) => Awaited<T>): Promise<Serialized<T>[]>;
|
||||||
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
|
public broadcastEval<T>(fn: (client: Client) => Awaited<T>, options: { shard: number }): Promise<Serialized<T>>;
|
||||||
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
|
|
||||||
public broadcastEval<T, P>(
|
public broadcastEval<T, P>(
|
||||||
fn: (client: Client, context: P) => T,
|
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
|
||||||
|
options: { context: P },
|
||||||
|
): Promise<Serialized<T>[]>;
|
||||||
|
public broadcastEval<T, P>(
|
||||||
|
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
|
||||||
options: { context: P; shard: number },
|
options: { context: P; shard: number },
|
||||||
): Promise<T>;
|
): Promise<Serialized<T>>;
|
||||||
public fetchClientValues(prop: string): Promise<any[]>;
|
public fetchClientValues(prop: string): Promise<any[]>;
|
||||||
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
||||||
public respawnAll(options?: MultipleShardRespawnOptions): Promise<void>;
|
public respawnAll(options?: MultipleShardRespawnOptions): Promise<void>;
|
||||||
@@ -1822,13 +1825,16 @@ declare module 'discord.js' {
|
|||||||
public totalShards: number | 'auto';
|
public totalShards: number | 'auto';
|
||||||
public shardList: number[] | 'auto';
|
public shardList: number[] | 'auto';
|
||||||
public broadcast(message: any): Promise<Shard[]>;
|
public broadcast(message: any): Promise<Shard[]>;
|
||||||
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
|
public broadcastEval<T>(fn: (client: Client) => Awaited<T>): Promise<Serialized<T>[]>;
|
||||||
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
|
public broadcastEval<T>(fn: (client: Client) => Awaited<T>, options: { shard: number }): Promise<Serialized<T>>;
|
||||||
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
|
|
||||||
public broadcastEval<T, P>(
|
public broadcastEval<T, P>(
|
||||||
fn: (client: Client, context: P) => T,
|
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
|
||||||
|
options: { context: P },
|
||||||
|
): Promise<Serialized<T>[]>;
|
||||||
|
public broadcastEval<T, P>(
|
||||||
|
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
|
||||||
options: { context: P; shard: number },
|
options: { context: P; shard: number },
|
||||||
): Promise<T>;
|
): Promise<Serialized<T>>;
|
||||||
public createShard(id: number): Shard;
|
public createShard(id: number): Shard;
|
||||||
public fetchClientValues(prop: string): Promise<any[]>;
|
public fetchClientValues(prop: string): Promise<any[]>;
|
||||||
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
||||||
@@ -4408,5 +4414,17 @@ declare module 'discord.js' {
|
|||||||
| 'STAGE_INSTANCE_UPDATE'
|
| 'STAGE_INSTANCE_UPDATE'
|
||||||
| 'STAGE_INSTANCE_DELETE';
|
| 'STAGE_INSTANCE_DELETE';
|
||||||
|
|
||||||
|
type Serialized<T> = T extends symbol | bigint | (() => unknown)
|
||||||
|
? never
|
||||||
|
: T extends number | string | boolean | undefined
|
||||||
|
? T
|
||||||
|
: T extends { toJSON(): infer R }
|
||||||
|
? R
|
||||||
|
: T extends ReadonlyArray<infer V>
|
||||||
|
? Serialized<V>[]
|
||||||
|
: T extends ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
|
||||||
|
? {}
|
||||||
|
: { [K in keyof T]: Serialized<T[K]> };
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,17 @@
|
|||||||
/// <reference path="index.d.ts" />
|
/// <reference path="index.d.ts" />
|
||||||
|
|
||||||
import { Client, Intents, Message, MessageAttachment, MessageEmbed } from 'discord.js';
|
import {
|
||||||
|
Client,
|
||||||
|
Collection,
|
||||||
|
Intents,
|
||||||
|
Message,
|
||||||
|
MessageAttachment,
|
||||||
|
MessageEmbed,
|
||||||
|
Permissions,
|
||||||
|
Serialized,
|
||||||
|
ShardClientUtil,
|
||||||
|
ShardingManager,
|
||||||
|
} from 'discord.js';
|
||||||
|
|
||||||
const client: Client = new Client({
|
const client: Client = new Client({
|
||||||
intents: Intents.NON_PRIVILEGED,
|
intents: Intents.NON_PRIVILEGED,
|
||||||
@@ -48,3 +59,42 @@ client.on('message', ({ channel }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
client.login('absolutely-valid-token');
|
client.login('absolutely-valid-token');
|
||||||
|
|
||||||
|
// Test type transformation:
|
||||||
|
declare const assertType: <T>(value: T) => asserts value is T;
|
||||||
|
declare const serialize: <T>(value: T) => Serialized<T>;
|
||||||
|
|
||||||
|
assertType<undefined>(serialize(undefined));
|
||||||
|
assertType<null>(serialize(null));
|
||||||
|
assertType<number[]>(serialize([1, 2, 3]));
|
||||||
|
assertType<{}>(serialize(new Set([1, 2, 3])));
|
||||||
|
assertType<{}>(
|
||||||
|
serialize(
|
||||||
|
new Map([
|
||||||
|
[1, '2'],
|
||||||
|
[2, '4'],
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
assertType<string>(serialize(new Permissions(Permissions.FLAGS.ATTACH_FILES)));
|
||||||
|
assertType<number>(serialize(new Intents(Intents.FLAGS.GUILDS)));
|
||||||
|
assertType<unknown>(
|
||||||
|
serialize(
|
||||||
|
new Collection([
|
||||||
|
[1, '2'],
|
||||||
|
[2, '4'],
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
assertType<never>(serialize(Symbol('a')));
|
||||||
|
assertType<never>(serialize(() => {}));
|
||||||
|
assertType<never>(serialize(BigInt(42)));
|
||||||
|
|
||||||
|
// Test type return of broadcastEval:
|
||||||
|
declare const shardClientUtil: ShardClientUtil;
|
||||||
|
declare const shardingManager: ShardingManager;
|
||||||
|
|
||||||
|
assertType<Promise<number[]>>(shardingManager.broadcastEval(() => 1));
|
||||||
|
assertType<Promise<number[]>>(shardClientUtil.broadcastEval(() => 1));
|
||||||
|
assertType<Promise<number[]>>(shardingManager.broadcastEval(async () => 1));
|
||||||
|
assertType<Promise<number[]>>(shardClientUtil.broadcastEval(async () => 1));
|
||||||
|
|||||||
Reference in New Issue
Block a user