Gathering detailed insights and metrics for @commander-js/extra-typings
Gathering detailed insights and metrics for @commander-js/extra-typings
Gathering detailed insights and metrics for @commander-js/extra-typings
Gathering detailed insights and metrics for @commander-js/extra-typings
Infer strong typings for commander options and action handlers
npm install @commander-js/extra-typings
98.9
Supply Chain
92
Quality
80.5
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
80 Stars
99 Commits
9 Forks
2 Watching
3 Branches
6 Contributors
Updated on 25 Nov 2024
TypeScript (93.13%)
JavaScript (6.87%)
Cumulative downloads
Total Downloads
Last day
-2.6%
80,543
Compared to previous day
Last week
7.5%
443,026
Compared to previous week
Last month
11.4%
2,018,259
Compared to previous month
Last year
305.7%
15,980,406
Compared to previous year
1
This package offers TypeScript typings for commander
which infer strong types for:
.opts()
The runtime is supplied by commander. This package is all about the typings.
Usage
@commander-js/extra-typings
using your preferred package managercommander
, if not already installed (peer dependency)@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
Command
, Argument
, or Option
(see subclass.test-d.ts)
this
Command
returns base class not subclass from .command(name)
Option
and Argument
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.
1import { program } from '@commander-js/extra-typings'; 2 3program.command('print') 4 .argument('<file>') 5 .option('--double-sided') 6 .action((targetFile, options) => { 7 // command-arguments and options are fully typed 8 });
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.
1import { Command } from '@commander-js/extra-typings'; 2 3// broken pattern 4const program = new Command(); // program type does not include options or arguments 5program.option('-d, --debug'); // adding option does not change type of program 6const options = program.opts(); // dumb type
1import { Command } from '@commander-js/extra-typings'; 2 3// working pattern 4const program = new Command() 5 .option('-d, --debug'); // program type includes chained options and arguments 6const options = program.opts(); // smart type
Use a "const assertion" on the choices to narrow the option type from string
:
1const program = new Command() 2 .addOption( 3 new Option('--drink-size <size>') 4 .choices(['small', 'medium', 'large'] as const) 5 // if you want to provide a default option, also add a const assertion to it 6 .default('medium' as const) 7 ).parse(); 8const drinkSize = program.opts().drinkSize; // "small" | "medium" | "large" | undefined
No vulnerabilities found.
No security vulnerabilities found.