Skip to content

Commit

Permalink
types: Strengthen autocomplete option types (#6950)
Browse files Browse the repository at this point in the history
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
  • Loading branch information
suneettipirneni and kyranet committed Nov 11, 2021
1 parent 9f240ea commit 7630158
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
15 changes: 14 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3313,7 +3313,6 @@ export interface BaseApplicationCommandOptionsData {
name: string;
description: string;
required?: boolean;
autocomplete?: boolean;
}

export interface UserApplicationCommandData extends BaseApplicationCommandData {
Expand Down Expand Up @@ -3346,14 +3345,27 @@ export interface ApplicationCommandChannelOption extends BaseApplicationCommandO
channelTypes?: (keyof typeof ChannelTypes)[];
}

export interface ApplicationCommandAutocompleteOption extends BaseApplicationCommandOptionsData {
type:
| 'STRING'
| 'NUMBER'
| 'INTEGER'
| ApplicationCommandOptionTypes.STRING
| ApplicationCommandOptionTypes.NUMBER
| ApplicationCommandOptionTypes.INTEGER;
autocomplete: true;
}

export interface ApplicationCommandChoicesData extends BaseApplicationCommandOptionsData {
type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoice[];
autocomplete?: false;
}

export interface ApplicationCommandChoicesOption extends BaseApplicationCommandOptionsData {
type: Exclude<CommandOptionChoiceResolvableType, ApplicationCommandOptionTypes>;
choices?: ApplicationCommandOptionChoice[];
autocomplete?: false;
}

export interface ApplicationCommandNumericOptionData extends ApplicationCommandChoicesData {
Expand Down Expand Up @@ -3403,6 +3415,7 @@ export type ApplicationCommandOptionData =
| ApplicationCommandNonOptionsData
| ApplicationCommandChannelOptionData
| ApplicationCommandChoicesData
| ApplicationCommandAutocompleteOption
| ApplicationCommandNumericOptionData
| ApplicationCommandSubCommandData;

Expand Down
67 changes: 60 additions & 7 deletions typings/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
CommandInteraction,
CommandInteractionOption,
CommandInteractionOptionResolver,
CommandOptionChoiceResolvableType,
CommandOptionNonChoiceResolvableType,
Constants,
ContextMenuInteraction,
Expand Down Expand Up @@ -447,6 +446,66 @@ client.on('ready', async () => {
// This is to check that stuff is the right type
declare const assertIsPromiseMember: (m: Promise<GuildMember>) => void;

const baseCommandOptionData = {
name: 'test',
description: 'test',
};

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'STRING',
// @ts-expect-error
autocomplete: true,
choices: [],
});

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'STRING',
autocomplete: false,
choices: [],
});

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'STRING',
choices: [],
});

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'NUMBER',
// @ts-expect-error
autocomplete: true,
choices: [],
});

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'INTEGER',
// @ts-expect-error
autocomplete: true,
choices: [],
});

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'NUMBER',
autocomplete: true,
});

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'STRING',
autocomplete: true,
});

assertType<ApplicationCommandOptionData>({
...baseCommandOptionData,
type: 'INTEGER',
autocomplete: true,
});

client.on('guildCreate', async g => {
const channel = g.channels.cache.random();
if (!channel) return;
Expand Down Expand Up @@ -820,12 +879,6 @@ declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
applicationNonChoiceOptionData.choices;
}

declare const applicationChoiceOptionData: ApplicationCommandOptionData & { type: CommandOptionChoiceResolvableType };
{
// Choices should be available.
applicationChoiceOptionData.choices;
}

declare const applicationSubGroupCommandData: ApplicationCommandSubGroupData;
{
assertType<'SUB_COMMAND_GROUP' | ApplicationCommandOptionTypes.SUB_COMMAND_GROUP>(
Expand Down

0 comments on commit 7630158

Please sign in to comment.