Skip to content
Myr

Add strong typing of .opts() using generics (#1539)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Co-authored-by: tyankatsu <frips.ryilsufupe+dev@gmail.com>

authored by John Gee · committed by GitHub · parent f9fe294 · Browse files · 2 files+12 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: tyankatsu <frips.ryilsufupe+dev@gmail.com>

Diff stats

2 files+12 added−3 removed

  • other:+12 added−3 removed

2 files+12 added−3 removed

modified:typings/index.d.ts+3 added-3 removed
481 unmodified lines
482 * or store separately (specify false). In both cases the option values can be accessed using .opts().483 *484 * @returns `this` command for chaining485 */486 storeOptionsAsProperties(): this & OptionValues;487 storeOptionsAsProperties(storeAsProperties: true): this & OptionValues;486 storeOptionsAsProperties<T extends OptionValues>(): this & T;487 storeOptionsAsProperties<T extends OptionValues>(storeAsProperties: true): this & T;488 storeOptionsAsProperties(storeAsProperties?: boolean): this;489490 /**491 * Retrieve option value.
101 unmodified lines
593594 /**595 * Return an object containing options as key-value pairs596 */597 opts(): OptionValues;597 opts<T extends OptionValues>(): T;598599 /**600 * Set the description.601 *
125 unmodified lines
modified:typings/index.test-d.ts+9 added
194 unmodified lines
195expectType<commander.OptionValues>(opts);196expectType(opts.foo);197expectType(opts['bar']);198199// opts with generics200interface MyCheeseOption {201 cheese: string;202}203const myCheeseOption = program.opts<MyCheeseOption>();204expectType<string>(myCheeseOption.cheese);205// @ts-expect-error Check that options strongly typed and does not allow arbitrary properties206expectType(myCheeseOption.foo);207208// description209expectType<commander.Command>(program.description('my description'));210expectType<string>(program.description());211expectType<commander.Command>(program.description('my description of command with arg foo', { foo: 'foo description'})); // deprecated
179 unmodified lines