Skip to content

Commit

Permalink
types: Allow choice's value type to be strictly inferred (#8529)
Browse files Browse the repository at this point in the history
* types: stricter types for choices in options

* test: add choice tests

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
Jiralite and kodiakhq[bot] committed Aug 20, 2022
1 parent 0dba8ad commit b3f7c32
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -3873,27 +3873,29 @@ export interface ApplicationCommandAutocompleteStringOptionData
autocomplete: true;
}

export interface ApplicationCommandChoicesData extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
export interface ApplicationCommandChoicesData<Type extends string | number = string | number>
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoiceData[];
choices?: ApplicationCommandOptionChoiceData<Type>[];
autocomplete?: false;
}

export interface ApplicationCommandChoicesOption extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
export interface ApplicationCommandChoicesOption<Type extends string | number = string | number>
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoiceData[];
choices?: ApplicationCommandOptionChoiceData<Type>[];
autocomplete?: false;
}

export interface ApplicationCommandNumericOptionData extends ApplicationCommandChoicesData {
export interface ApplicationCommandNumericOptionData extends ApplicationCommandChoicesData<number> {
type: CommandOptionNumericResolvableType;
minValue?: number;
min_value?: number;
maxValue?: number;
max_value?: number;
}

export interface ApplicationCommandStringOptionData extends ApplicationCommandChoicesData {
export interface ApplicationCommandStringOptionData extends ApplicationCommandChoicesData<string> {
type: ApplicationCommandOptionType.String;
minLength?: number;
min_length?: number;
Expand All @@ -3905,13 +3907,13 @@ export interface ApplicationCommandBooleanOptionData extends BaseApplicationComm
type: ApplicationCommandOptionType.Boolean;
}

export interface ApplicationCommandNumericOption extends ApplicationCommandChoicesOption {
export interface ApplicationCommandNumericOption extends ApplicationCommandChoicesOption<number> {
type: CommandOptionNumericResolvableType;
minValue?: number;
maxValue?: number;
}

export interface ApplicationCommandStringOption extends ApplicationCommandChoicesOption {
export interface ApplicationCommandStringOption extends ApplicationCommandChoicesOption<string> {
type: ApplicationCommandOptionType.String;
minLength?: number;
maxLength?: number;
Expand Down Expand Up @@ -3953,7 +3955,6 @@ export type ApplicationCommandOptionData =
| ApplicationCommandSubGroupData
| ApplicationCommandNonOptionsData
| ApplicationCommandChannelOptionData
| ApplicationCommandChoicesData
| ApplicationCommandAutocompleteNumericOptionData
| ApplicationCommandAutocompleteStringOptionData
| ApplicationCommandNumericOptionData
Expand All @@ -3970,7 +3971,6 @@ export type ApplicationCommandOption =
| ApplicationCommandAutocompleteStringOption
| ApplicationCommandNonOptions
| ApplicationCommandChannelOption
| ApplicationCommandChoicesOption
| ApplicationCommandNumericOption
| ApplicationCommandStringOption
| ApplicationCommandRoleOption
Expand All @@ -3980,10 +3980,10 @@ export type ApplicationCommandOption =
| ApplicationCommandAttachmentOption
| ApplicationCommandSubCommand;

export interface ApplicationCommandOptionChoiceData {
export interface ApplicationCommandOptionChoiceData<Value extends string | number = string | number> {
name: string;
nameLocalizations?: LocalizationMap;
value: string | number;
value: Value;
}

export interface ApplicationCommandPermissions {
Expand Down
19 changes: 19 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Expand Up @@ -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;
{
Expand All @@ -1121,6 +1122,24 @@ declare const applicationCommandManager: ApplicationCommandManager;
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(
applicationCommandManager.set([applicationCommandData], '0'),
);

// Test inference of choice values.
if ('choices' in applicationCommandOptionData) {
if (applicationCommandOptionData.type === ApplicationCommandOptionType.String) {
expectType<string>(applicationCommandOptionData.choices[0]!.value);
expectNotType<number>(applicationCommandOptionData.choices[0]!.value);
}

if (applicationCommandOptionData.type === ApplicationCommandOptionType.Integer) {
expectType<number>(applicationCommandOptionData.choices[0]!.value);
expectNotType<string>(applicationCommandOptionData.choices[0]!.value);
}

if (applicationCommandOptionData.type === ApplicationCommandOptionType.Number) {
expectType<number>(applicationCommandOptionData.choices[0]!.value);
expectNotType<string>(applicationCommandOptionData.choices[0]!.value);
}
}
}

declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
Expand Down

0 comments on commit b3f7c32

Please sign in to comment.