#Infering types of a builder
1 messages · Page 1 of 1 (latest)
i mean, it would be nice to have intellisense (in the case of a command ) of the subcommands registered, the options addeds ans its values when defining the execution/run/callback method for specific command
But… you don’t access the command itself anywhere in there, you access the interaction you received
this wont make sense but explains my point or something like that:
type CommandDefinition<T>= {
data: SlashCommandBuilder<T>
execution<T>():Promise<unknown>
}
export default {
data: whatever,
execution: whatever
} satisfies CommandDefinition
yes i know but the interaction correspond to the command/subcommand definition
like the options recived, etc
Not really. A ChatInputCommandInteraction doesn’t have any special type depending on the provided options, especially because optional options exist etc.
So what exactly is your goal with your typings?
a simple example would be if a created a command with a requited option with name user-to-mention of a role
it woud be great that when resolving the interaction that when a selected interacion.options.getRole it show autocompletes with user-to-mention or when accesing interaction.options.data
i mean that the interaction corresponds to the command that is resolving
or for example if i create a command with subcommands: name-1 name-2 name-3
when i get the subcommand it would be nice to has it typed as name-1 | name-2 | name-3 and based on what it is using the inference of ts to have the corresponding definition of the interaction for the command
You‘re expecting something that isn’t possible…
i tried something fast, i coudnt get it working but with a wit of ts magic i'm sure its possible
this is the try:
type Command = {
name: string,
options: {
name: string,
type: number,
value: string
}[]
}
type Interaction<T extends Command> = {
guild: any,
options: T['options']
}
type CommandDefinition<T extends Command = Command> = {
data: T,
execute: ({interaction}:{interaction:Interaction<T>}) => Promise<unknown>
}
const command = {
data: {
name: 'test',
options: [
{
name: 'text',
type: 3,
value: 'string'
}
]
},
async execute({interaction}) {
interaction.options[0].name
interaction.options[1].name
}
} satisfies CommandDefinition
obviously its not the correct types but for the idea kinda represents the case