Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@commander-js/extra-typings

commander-js2.3mMIT13.0.0TypeScript support: included

Infer strong typings for commander options and action handlers

readme

extra-typings for commander

NPM Version NPM Downloads

This package offers TypeScript typings for commander which infer strong types for:

  • all the parameters of the action handler, including the options
  • options returned by .opts()

This package requires TypeScript 5.0 or higher.

The runtime is supplied by commander. This package is all about the typings.

Usage

  • install @commander-js/extra-typings using your preferred package manager
  • install commander, if not already installed (peer dependency)
  • in code import from @commander-js/extra-typings instead of commander

The installed version of this package should match the major and minor version numbers of the installed commander package, but the patch version number is independent (following pattern used by Definitely Typed).

Credit: this builds on work by @PaperStrike in https://github.com/tj/commander.js/pull/1758

Limitations

  • the generics lead to some noisy types visible in editor and errors
  • some minor code changes required for subclasses of Command, Argument, or Option (see subclass.test-d.ts)
    • chaining methods which do type inference return base class rather than this
    • subclass of Command returns base class not subclass from .command(name)
    • type parameter needed for class declaration of subclass of Option and Argument

Usage tips

The types are built up as the options and arguments are defined. The usage pattern for action handlers is easy. Just chain the action handler after the options and arguments.

import { program } from '@commander-js/extra-typings';

program.command('print')
  .argument('<file>')
  .option('--double-sided')
  .action((targetFile, options) => {
    // command-arguments and options are fully typed
  });

For working with a single command without an action handler, the configuration need to be done at the same time as the variable is declared.

import { Command } from '@commander-js/extra-typings';

// broken pattern
const program = new Command(); // program type does not include options or arguments
program.option('-d, --debug'); // adding option does not change type of program
const options = program.opts(); // dumb type
import { Command } from '@commander-js/extra-typings';

// working pattern
const program = new Command()
  .option('-d, --debug'); // program type includes chained options and arguments
const options = program.opts(); // smart type

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog.

The version numbering does not follow semantic versioning but instead aligns with the commander version number. The installed version of this package should match the major and minor version numbers of the installed commander package, but the patch version number is independent (following pattern used by Definitely Typed).

13.0.0 (2024-12-31)

Added

  • strongly type .optsWithGlobals() to include inferred globals (#78)
  • weakly type .getOptionValueSourceWithGlobals() to include inferred globals (#78)
  • infer narrow types for choices, so no longer need to specify as const (#79)

Changed

  • Breaking: Typescript 5.0 or higher is required (#79)

13.0.0-1 (2024-12-08)

(Released in 13.0.0)

13.0.0-0 (2024-12-08) [YANKED]

Published from wrong branch.

12.1.0 (2024-05-18)

Changed

  • use ESLint for linting (#65)
  • format source files with Prettier (#66)

Removed

  • removed unimplemented Option.fullDescription from TypeScript definition (#70)

12.0.1 (2023-03-02)

Changed

  • include implicit this in parameters for action handler callback #59

12.0.0 (2023-02-04)

Changed

  • update peerDependencies to `commander@12.0.x`, which requires Node.js v18 or higher

12.0.0-1 (2023-11-13)

Fixed

  • use prerelease Commander version for peerDependencies

12.0.0-0 (2023-11-12)

Changed

  • update peerDependencies to `commander@12.0.x`, which requires Node.js v18 or higher

11.1.0 (2023-10-15)

Added

  • Option properties: envVar, presetArg (#48)
  • Argument properties: argChoices, defaultValue, defaultValueDescription (#48)
  • Command properties: options, registeredArguments (#50)

Changed

Fixed

  • remove unused Option.optionFlags property (#48)
  • add that Command.version() can also be used as getter (#48)
  • add null return type to Commands.executableDir(), for when not configured (#48)
  • preserve option typings when adding arguments to Command (#49)

11.0.0 (2023-06-16)

Changed

  • update peerDependencies to `commander@11.0.x`, which requires Node.js v16 or higher

10.0.3 (2023-03-03)

Added

  • narrow types based on .choices() (#29)

Fixed

  • improve Option type inferences for certain combinations of configuration (#31)
  • mark .action() callback as allowing promises (#33)

10.0.2 (2023-01-27)

Fixed

  • createOption passes through description into object constructor. (#25)

10.0.1 (2023-01-17)

Fixed

  • createCommand, createArgument, and createOption pass through arguments into object constructors. (#23)

10.0.0 (2023-01-14)

Changed

  • update peerDependencies to `commander@10.0.x`, which requires Node.js v14 or higher

9.5.0 (2023-01-07)

Added

  • .getOptionValueSourceWithGlobals() (#18)
  • showGlobalOptions for .configureHelp{} and Help (#19)

Changed

9.4.1 (2022-11-01)

Fixed

  • added esm.mjs to package (#16)

9.4.0 (2022-10-28)

Added

  • type processedArgs
  • infer types from .createOption()
  • infer types from .createArgument()

Changed

0.3.0 (2022-09-07)

Added

  • add CommandUnknownOpts for when Command not strongly typed
  • use CommandUnknownOpts throughout Help, so can pass in commands which are Command or CommandUnknownOpts
  • use CommandUnknownOpts with addCommand
  • type .hook() arguments
  • add inferred option names and types to .getOptionValue(), but allow unknown and return unknown
  • add implied to OptionValueSource

Changed

  • switch OptionValues to returning unknown (goodbye another any!)
  • allow user defined source on get and set

0.2.0 (2022-08-23)

Fixed

  • a missing variadic optional command-line argument should be [] not undefined

Changed

  • use simple array type rather than fancy non-empty tuple, like string[] rather than [string, ...string[]]

0.1.0 (2022-08-16)

Added

  • infer types from
    • .argument()
    • .arguments()
    • .addArgument()
    • .command()
    • .option()
    • .addOption()
  • inferred types for .action()
  • inferred types for .opts()