Skip to content

Commit

Permalink
set this context for action callback (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthyk authored Feb 12, 2024
1 parent c09ce23 commit ca47a6b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ export class CommanderError extends Error {
*
* @returns `this` command for chaining
*/
action(fn: (...args: [...Args, Opts, this]) => void | Promise<void>): this;
action(fn: (this: this, ...args: [...Args, Opts, this]) => void | Promise<void>): this;

/**
* Define option with `flags`, `description`, and optional argument parsing function or `defaultValue` or both.
Expand Down
10 changes: 10 additions & 0 deletions tests/arguments.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@ program
expectAssignable<OptionValues>(options);
});

// `this` should be current Command
program
.option('-o, --opt')
.option('-v, --verbose')
.argument('<arg>')
.argument('<second argument>')
.action(function () {
expectType<Command<[string, string], { opt?: true, verbose?: true }>>(this)
})

// default type ignored when arg is required
expectType<('C')>(
program
Expand Down

0 comments on commit ca47a6b

Please sign in to comment.