From ca47a6bca30b64750f102a3019c0e9fe92afc867 Mon Sep 17 00:00:00 2001 From: Matthias Keckl <53833818+matthyk@users.noreply.github.com> Date: Mon, 12 Feb 2024 06:55:55 +0100 Subject: [PATCH] set `this` context for action callback (#59) --- index.d.ts | 2 +- tests/arguments.test-d.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index ee39351..4ae9a9e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -691,7 +691,7 @@ export class CommanderError extends Error { * * @returns `this` command for chaining */ - action(fn: (...args: [...Args, Opts, this]) => void | Promise): this; + action(fn: (this: this, ...args: [...Args, Opts, this]) => void | Promise): this; /** * Define option with `flags`, `description`, and optional argument parsing function or `defaultValue` or both. diff --git a/tests/arguments.test-d.ts b/tests/arguments.test-d.ts index 76d7aed..c9bdf12 100644 --- a/tests/arguments.test-d.ts +++ b/tests/arguments.test-d.ts @@ -314,6 +314,16 @@ program expectAssignable(options); }); +// `this` should be current Command +program + .option('-o, --opt') + .option('-v, --verbose') + .argument('') + .argument('') + .action(function () { + expectType>(this) + }) + // default type ignored when arg is required expectType<('C')>( program