Skip to content

Instantly share code, notes, and snippets.

@dsmrt
Last active October 15, 2025 22:13
Show Gist options
  • Save dsmrt/cf1812cb657738e83d76ba13ca28dbde to your computer and use it in GitHub Desktop.
Save dsmrt/cf1812cb657738e83d76ba13ca28dbde to your computer and use it in GitHub Desktop.
Yargs Typescript CommandModule Generics
import { CommandModule, Argv, Options, ArgumentsCamelCase } from 'yargs'
export interface MyOptions extends Options {
interactive: boolean
env: string
color: string
}
export class MyCommand<U extends MyOptions> implements CommandModule<{}, U> {
public command = 'my-cmd'
public describe = 'Just a simple generics example'
public builder = (args: Argv): Argv<U> => {
args.option('interactive', { boolean: true, alias: 'i', describe: 'get interactive with it!' })
args.option('env', { string: true, alias: 'e', describe: 'Environment' })
args.option('color', { boolean: true, describe: 'disable color', default: true })
return args as unknown as Argv<U>
}
public handler = async (args: ArgumentsCamelCase<U>) => {
// type hinting works with the following!
console.log(
args.color,
args.env,
args.interactive
)
}
}
@dsmrt
Copy link
Author

dsmrt commented Feb 25, 2021

I spent way too much time trying to figure this out. This simple version on how to get type hinting working using yargs Typescript generics.

See my screen shot on the type hinting in vscode.

Screen Shot 2021-02-24 at 8 28 21 PM

@bentonmize
Copy link

This was a lifesaver... 👍

@kitschpatrol
Copy link

Thank you!

@i-yusuf
Copy link

i-yusuf commented Aug 24, 2024

import { CommandModule, Arguments, Argv } from 'yargs';

interface CommandArgs {
  arg?: string;
}

export class ConfigureCommand implements CommandModule {
  public command = '';
  public describe = '';

  public builder(args: Argv): Argv<CommandArgs> {
    return args.positional('arg', {
      describe: 'Hello world!',
      type: 'string',
    });
  }

  public handler(argv: Arguments<CommandArgs>) {
    console.log(argv);
  }
}

or

import { CommandModule, Arguments, Argv } from 'yargs';

interface CommandArgs {
  arg?: string;
}

export class ConfigureCommand implements CommandModule<{}, CommandArgs> {
  public command = '';

  public describe = '';

  public builder(args: Argv<{}>) {
    return args.positional('arg', {
      describe: 'Hello world!',
      type: 'string',
    });
  }

  public handler(argv: Arguments<CommandArgs>) {
    console.log(argv);
  }
}

@giseburt
Copy link

This is what I've come up with to reduce the duplication of the interface and code in builder(), and the trick is the satisfies Record<string, Options> with InferredOptionTypes :

import type { CommandModule, Arguments, Argv, Options, InferredOptionTypes } from "yargs";

const options = {
  arg: {
    describe: "Hello world!",
    type: "string",
  },
} as const satisfies Record<string, Options>;
type OptionsType = typeof options;
type CommandArgs = InferredOptionTypes<OptionsType>;

export class ConfigureCommand implements CommandModule<{}, CommandArgs> {
  public command = "";

  public describe = "";

  public builder(args: Argv) {
    return args.options(options);
  }

  public handler({ arg }: Arguments<CommandArgs>) {
    console.log(arg);
  }
}

Note that this is with yargs@18.

The satisfies locks the options to those types at compile time, and has the added benefit of giving docs links to the sub values:
image

It keeps the typing in the handler, so you can deconstruct it, and you can see it even retains docstrings (but not describe - oh well):
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment