Skip to content

fix: slash command handler subcommands keys aren't type-safe #56

@neolectron

Description

@neolectron

Problem

Subcommands keys aren't typechecked.

{
  schema: new SlashCommandBuilder()
    .setName('myCommand')
    .addSubcommand((subcommand) =>
      subcommand.setName('subcommand1').setDescription('do 1'),
    )
    .addSubcommand((subcommand) =>
      subcommand
        .setName('subcommand2')
        .setDescription('do 2'),
    )
    .toJSON(),
  handler: {
    somethingNotLegit: async (interaction) => {},
    anyKeyCanBeHere: async (interaction) => {},
	// ^ ------------ Not legit
  },
}

Solution

Override the type of schema with subcommands strings as const (or check if they aren't in the type already). Then retrieve the subcommands keys with a type helper.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions