types: don't allow any object in the first parameter if second parameter is not given in TextBasedChannel#send (#4736)

This commit is contained in:
cherryblossom000
2020-08-29 20:08:04 +10:00
committed by GitHub
parent 74ebb650df
commit 74763ef3fb
2 changed files with 75 additions and 41 deletions

View File

@@ -1,6 +1,6 @@
/// <reference path="index.d.ts" />
import { Client } from 'discord.js';
import { Client, Message, MessageAttachment, MessageEmbed } from 'discord.js';
const client: Client = new Client();
@@ -25,4 +25,29 @@ client.on('messageReactionRemoveAll', async message => {
console.log(`messageReactionRemoveAll - content: ${message.content}`);
});
// These are to check that stuff is the right type
declare const assertIsMessage: (m: Promise<Message>) => void;
declare const assertIsMessageArray: (m: Promise<Message[]>) => void;
client.on('message', ({ channel }) => {
assertIsMessage(channel.send('string'));
assertIsMessage(channel.send({}));
assertIsMessage(channel.send({ embed: {} }));
assertIsMessage(channel.send({ another: 'property' }, {}));
const attachment = new MessageAttachment('file.png');
const embed = new MessageEmbed();
assertIsMessage(channel.send(attachment));
assertIsMessage(channel.send(embed));
assertIsMessage(channel.send([attachment, embed]));
assertIsMessageArray(channel.send(Symbol('another primitive'), { split: true }));
assertIsMessageArray(channel.send({ split: true }));
// @ts-expect-error
channel.send();
// @ts-expect-error
channel.send({ another: 'property' });
});
client.login('absolutely-valid-token');