From b3f7c32f7f91f12766178f5e17585856e81d9a87 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Sat, 20 Aug 2022 21:29:16 +0100 Subject: [PATCH] types: Allow choice's value type to be strictly inferred (#8529) * types: stricter types for choices in options * test: add choice tests Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/discord.js/typings/index.d.ts | 24 ++++++++++----------- packages/discord.js/typings/index.test-d.ts | 19 ++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index ac715ff647c5..61e6935a78a9 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -3873,19 +3873,21 @@ export interface ApplicationCommandAutocompleteStringOptionData autocomplete: true; } -export interface ApplicationCommandChoicesData extends Omit { +export interface ApplicationCommandChoicesData + extends Omit { type: CommandOptionChoiceResolvableType; - choices?: ApplicationCommandOptionChoiceData[]; + choices?: ApplicationCommandOptionChoiceData[]; autocomplete?: false; } -export interface ApplicationCommandChoicesOption extends Omit { +export interface ApplicationCommandChoicesOption + extends Omit { type: CommandOptionChoiceResolvableType; - choices?: ApplicationCommandOptionChoiceData[]; + choices?: ApplicationCommandOptionChoiceData[]; autocomplete?: false; } -export interface ApplicationCommandNumericOptionData extends ApplicationCommandChoicesData { +export interface ApplicationCommandNumericOptionData extends ApplicationCommandChoicesData { type: CommandOptionNumericResolvableType; minValue?: number; min_value?: number; @@ -3893,7 +3895,7 @@ export interface ApplicationCommandNumericOptionData extends ApplicationCommandC max_value?: number; } -export interface ApplicationCommandStringOptionData extends ApplicationCommandChoicesData { +export interface ApplicationCommandStringOptionData extends ApplicationCommandChoicesData { type: ApplicationCommandOptionType.String; minLength?: number; min_length?: number; @@ -3905,13 +3907,13 @@ export interface ApplicationCommandBooleanOptionData extends BaseApplicationComm type: ApplicationCommandOptionType.Boolean; } -export interface ApplicationCommandNumericOption extends ApplicationCommandChoicesOption { +export interface ApplicationCommandNumericOption extends ApplicationCommandChoicesOption { type: CommandOptionNumericResolvableType; minValue?: number; maxValue?: number; } -export interface ApplicationCommandStringOption extends ApplicationCommandChoicesOption { +export interface ApplicationCommandStringOption extends ApplicationCommandChoicesOption { type: ApplicationCommandOptionType.String; minLength?: number; maxLength?: number; @@ -3953,7 +3955,6 @@ export type ApplicationCommandOptionData = | ApplicationCommandSubGroupData | ApplicationCommandNonOptionsData | ApplicationCommandChannelOptionData - | ApplicationCommandChoicesData | ApplicationCommandAutocompleteNumericOptionData | ApplicationCommandAutocompleteStringOptionData | ApplicationCommandNumericOptionData @@ -3970,7 +3971,6 @@ export type ApplicationCommandOption = | ApplicationCommandAutocompleteStringOption | ApplicationCommandNonOptions | ApplicationCommandChannelOption - | ApplicationCommandChoicesOption | ApplicationCommandNumericOption | ApplicationCommandStringOption | ApplicationCommandRoleOption @@ -3980,10 +3980,10 @@ export type ApplicationCommandOption = | ApplicationCommandAttachmentOption | ApplicationCommandSubCommand; -export interface ApplicationCommandOptionChoiceData { +export interface ApplicationCommandOptionChoiceData { name: string; nameLocalizations?: LocalizationMap; - value: string | number; + value: Value; } export interface ApplicationCommandPermissions { diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index 4b18299d56e2..995c60ae4b56 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -1102,6 +1102,7 @@ expectAssignable<'death'>(ShardEvents.Death); expectAssignable<1>(Status.Connecting); declare const applicationCommandData: ApplicationCommandData; +declare const applicationCommandOptionData: ApplicationCommandOptionData; declare const applicationCommandResolvable: ApplicationCommandResolvable; declare const applicationCommandManager: ApplicationCommandManager; { @@ -1121,6 +1122,24 @@ declare const applicationCommandManager: ApplicationCommandManager; expectType>>( applicationCommandManager.set([applicationCommandData], '0'), ); + + // Test inference of choice values. + if ('choices' in applicationCommandOptionData) { + if (applicationCommandOptionData.type === ApplicationCommandOptionType.String) { + expectType(applicationCommandOptionData.choices[0]!.value); + expectNotType(applicationCommandOptionData.choices[0]!.value); + } + + if (applicationCommandOptionData.type === ApplicationCommandOptionType.Integer) { + expectType(applicationCommandOptionData.choices[0]!.value); + expectNotType(applicationCommandOptionData.choices[0]!.value); + } + + if (applicationCommandOptionData.type === ApplicationCommandOptionType.Number) { + expectType(applicationCommandOptionData.choices[0]!.value); + expectNotType(applicationCommandOptionData.choices[0]!.value); + } + } } declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {