Type safe Slash Commands Options ?

Hi there, I can't seem to find a way to add Type Safe options to a Slash command. See Example:
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder //
.setName(this.name)
.setDescription(this.description)
.addStringOption((builder) => {
return builder
.setName('difficulty')
.setDescription('Choose a difficulty')
.addChoices({ name: 'Easy', value: 'easy' }, { name: 'Medium', value: 'medium' }, { name: 'Hard', value: 'hard' });
})
);
}
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder //
.setName(this.name)
.setDescription(this.description)
.addStringOption((builder) => {
return builder
.setName('difficulty')
.setDescription('Choose a difficulty')
.addChoices({ name: 'Easy', value: 'easy' }, { name: 'Medium', value: 'medium' }, { name: 'Hard', value: 'hard' });
})
);
}
I have a Type called type Difficulty = 'easy' | 'medium' | 'hard' and functions that expect this type. But the interaction.options.getString('difficulty', false) method simply returns string. Which means I need to add as Difficulty to it. And I feel like it defeats a little bit the purpose of TypeScript when I do this. So I was wondering if there was a better way to handle this, or should I just rely on Runtime checking ? Thanks ❤️
1 Reply
Favna
Favna15mo ago
You'll have to cast it. Discordjs doesn't offer a way to make it type safe directly. Just add as Difficulty
Want results from more Discord servers?
Add your server