Skip to content

Commit

Permalink
fix(Sharding): strict type context and return
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Jun 28, 2021
1 parent 03d3a5c commit 306cc90
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
38 changes: 28 additions & 10 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1756,13 +1756,16 @@ declare module 'discord.js' {
public readonly ids: number[];
public mode: ShardingManagerMode;
public parentPort: any | null;
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: P) => T,
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P },
): Promise<Serialized<T>[]>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P; shard: number },
): Promise<T>;
): Promise<Serialized<T>>;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
public respawnAll(options?: MultipleShardRespawnOptions): Promise<void>;
Expand All @@ -1785,13 +1788,16 @@ declare module 'discord.js' {
public totalShards: number | 'auto';
public shardList: number[] | 'auto';
public broadcast(message: any): Promise<Shard[]>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P },
): Promise<Serialized<T>[]>;
public broadcastEval<T, P>(
fn: (client: Client, context: P) => T,
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P; shard: number },
): Promise<T>;
): Promise<Serialized<T>>;
public createShard(id: number): Shard;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
Expand Down Expand Up @@ -4321,5 +4327,17 @@ declare module 'discord.js' {
| 'STAGE_INSTANCE_UPDATE'
| '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
}
40 changes: 39 additions & 1 deletion typings/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
/// <reference path="index.d.ts" />

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,
Expand Down Expand Up @@ -48,3 +57,32 @@ client.on('message', ({ channel }) => {
});

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

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)));

0 comments on commit 306cc90

Please sign in to comment.