mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 02:23:31 +01:00
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>
This commit is contained in:
24
packages/discord.js/typings/index.d.ts
vendored
24
packages/discord.js/typings/index.d.ts
vendored
@@ -3873,19 +3873,21 @@ 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;
|
||||
@@ -3893,7 +3895,7 @@ export interface ApplicationCommandNumericOptionData extends ApplicationCommandC
|
||||
max_value?: number;
|
||||
}
|
||||
|
||||
export interface ApplicationCommandStringOptionData extends ApplicationCommandChoicesData {
|
||||
export interface ApplicationCommandStringOptionData extends ApplicationCommandChoicesData<string> {
|
||||
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<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;
|
||||
@@ -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<Value extends string | number = string | number> {
|
||||
name: string;
|
||||
nameLocalizations?: LocalizationMap;
|
||||
value: string | number;
|
||||
value: Value;
|
||||
}
|
||||
|
||||
export interface ApplicationCommandPermissions {
|
||||
|
||||
@@ -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<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 & {
|
||||
|
||||
Reference in New Issue
Block a user