Skip to content

Commit

Permalink
fix: unwrap promises
Browse files Browse the repository at this point in the history
Also added tests to make sure they work
  • Loading branch information
kyranet committed Jun 28, 2021
1 parent 306cc90 commit 52ec31f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 9 additions & 9 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ declare enum WebhookTypes {
'Channel Follower' = 2,
}

type Awaited<T> = T | Promise<T>;
type Awaited<T> = T | PromiseLike<T>;

declare module 'discord.js' {
import BaseCollection from '@discordjs/collection';
Expand Down Expand Up @@ -1756,14 +1756,14 @@ declare module 'discord.js' {
public readonly ids: number[];
public mode: ShardingManagerMode;
public parentPort: any | null;
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>(fn: (client: Client) => Awaited<T>): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => Awaited<T>, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<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>) => T,
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
options: { context: P; shard: number },
): Promise<Serialized<T>>;
public fetchClientValues(prop: string): Promise<any[]>;
Expand All @@ -1788,14 +1788,14 @@ 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<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T>(fn: (client: Client) => Awaited<T>): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => Awaited<T>, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<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>) => T,
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
options: { context: P; shard: number },
): Promise<Serialized<T>>;
public createShard(id: number): Shard;
Expand Down
12 changes: 12 additions & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
MessageEmbed,
Permissions,
Serialized,
ShardClientUtil,
ShardingManager,
} from 'discord.js';

const client: Client = new Client({
Expand Down Expand Up @@ -58,6 +60,7 @@ client.on('message', ({ channel }) => {

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

Expand Down Expand Up @@ -86,3 +89,12 @@ assertType<unknown>(
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));

0 comments on commit 52ec31f

Please sign in to comment.