#Infering types of a builder

1 messages · Page 1 of 1 (latest)

keen mesa
#

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

kindred sierra
#

But… you don’t access the command itself anywhere in there, you access the interaction you received

keen mesa
#

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
keen mesa
#

like the options recived, etc

kindred sierra
#

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?

keen mesa
#

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

kindred sierra
#

You‘re expecting something that isn’t possible…

keen mesa
#

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

kindred sierra
#

And how do you intend to turn a ChatInputCommandInteraction you receive into that type you want?

#

Seems like you‘re about to write your own library at that point

keen mesa
#

i could try to implement that type with a generic later if you want the concep

#

i only want to improve the dev experience