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

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

Merged
merged 1 commit into from May 30, 2021
Merged
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 @@ -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
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