Skip to content

Commit

Permalink
Add strong typing of .opts() using generics (#1539)
Browse files Browse the repository at this point in the history
Co-authored-by: tyankatsu <frips.ryilsufupe+dev@gmail.com>


Co-authored-by: tyankatsu <frips.ryilsufupe+dev@gmail.com>
  • Loading branch information
shadowspawn and tyankatsu0105 authored May 30, 2021
1 parent f9fe294 commit 03d2147
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@ export class Command {
*
* @returns `this` command for chaining
*/
storeOptionsAsProperties(): this & OptionValues;
storeOptionsAsProperties(storeAsProperties: true): this & OptionValues;
storeOptionsAsProperties<T extends OptionValues>(): this & T;
storeOptionsAsProperties<T extends OptionValues>(storeAsProperties: true): this & T;
storeOptionsAsProperties(storeAsProperties?: boolean): this;

/**
Expand Down Expand Up @@ -594,7 +594,7 @@ export class Command {
/**
* Return an object containing options as key-value pairs
*/
opts(): OptionValues;
opts<T extends OptionValues>(): T;

/**
* Set the description.
Expand Down
9 changes: 9 additions & 0 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ expectType<commander.OptionValues>(opts);
expectType(opts.foo);
expectType(opts['bar']);

// opts with generics
interface MyCheeseOption {
cheese: string;
}
const myCheeseOption = program.opts<MyCheeseOption>();
expectType<string>(myCheeseOption.cheese);
// @ts-expect-error Check that options strongly typed and does not allow arbitrary properties
expectType(myCheeseOption.foo);

// description
expectType<commander.Command>(program.description('my description'));
expectType<string>(program.description());
Expand Down

0 comments on commit 03d2147

Please sign in to comment.