Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add generics to OptionValues #1537

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions typings/index.d.ts
Expand Up @@ -384,8 +384,8 @@ declare namespace commander {
*
* @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 @@ -485,7 +485,7 @@ declare namespace commander {
/**
* 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
Expand Up @@ -171,6 +171,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