fix(Sharding): strict type context and return (#5933)

This commit is contained in:
Antonio Román
2021-06-29 21:36:04 +02:00
committed by GitHub
parent 87e8cdd3eb
commit 1925d01d8f
2 changed files with 80 additions and 12 deletions

View File

@@ -1,6 +1,17 @@
/// <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({
intents: Intents.NON_PRIVILEGED,
@@ -48,3 +59,42 @@ 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>;
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));