Skip to Content
nyx logonyx guide

Overview

Reading time:3 min read

The @nyx-discord/typed-options package types the options of your slash commands from the command data itself, so a misspelled name, a wrong option kind or a missing null check becomes a compile error.

Without it, interaction.options.getString('reason') accepts any name, ignores required and choices, and returns string | null even for options Discord always sends. With it, the getters are checked against the data you already write.

It doesn't depend on the rest of nyx: it only needs discord-api-types. You can use it in any bot that uses discord.js, @discordjs/core or raw API payloads, with or without nyx.

Installation

Installing

npm i @nyx-discord/typed-options

It has discord-api-types as a peer dependency, and discord.js as an optional one for the /djs entry point.

Defining the command

Create the command data with chatInput() and one helper per option kind. The result is plain API JSON, so you register it with any library, or use it as the data of a nyx command:

import { , ,  } from '@nyx-discord/typed-options';

const  = ({
  : 'ban',
  : 'Ban a member',
  : {
    : ({ : 'Who to ban', : true }),
    : ({ : 'Why' }),
  },
});

Reading the options

Pass the data and the interaction to typedOptions(). The entry point you import it from depends on your library:

import {  } from '@nyx-discord/typed-options/djs';

const  = (, );

const target = .('target');
const target: User
const reason = .('reason');
const reason: string | null

A mistake in the name, or in the kind of option, doesn't compile:

const  = (, );

// Error! There is no option named "reasn"
.('reasn');
Argument of type '"reasn"' is not assignable to parameter of type '"reason"'.
// Error! "target" is a user option, not a string .('target');
Argument of type '"target"' is not assignable to parameter of type '"reason"'.

Entry points

Entry pointPurpose
@nyx-discord/typed-optionschatInput(), subcommand(), group(), the option helpers, and the types of the getters. Doesn't depend on any Discord library.
@nyx-discord/typed-options/djstypedOptions() for discord.js. It returns interaction.options with narrower types, so it costs nothing at runtime.
@nyx-discord/typed-options/djs-coretypedOptions() for raw API payloads. It takes the @discordjs/core event props or a bare interaction payload, so it also covers the HTTP transport and other libraries.

Using it in a nyx command

Read the options from the command's own data:

import type {  } from 'discord.js';
import {  } from '@nyx-discord/djs';
import { , ,  } from '@nyx-discord/typed-options';
import {  } from '@nyx-discord/typed-options/djs';

class  extends  {
  protected readonly  = ({
    : 'ban',
    : 'Ban a member',
    : {
      : ({ : 'Who to ban', : true }),
      : ({ : 'Why' }),
    },
  });

  public async (: ) {
    const  = (this., );
    await .(`Banning ${.('target').}`);
  }
}

Builders like SlashCommandBuilder can't carry literal types, so they keep working but aren't typed. Use chatInput() for the commands whose options you want checked.

On this page