Skip to content

Support subclassing Command #1002

@ngocdaothanh

Description

@ngocdaothanh

Currently, the return type of the methods in class Command is Command:

public command(name: string, desc?: string, opts?: commander.CommandOptions): Command;

I cannot extend Command to add more methods, like this:

class MyCommandWithUtilities extends Command {
  public util1(): this {
     // ...
     return this;
  }
}

const cmd = new MyCommandWithUtilities();
cmd
  .command('foo')
  .util1()  // Syntax error, because `command` returns `Command`, not `MyCommandWithUtilities`
  .option(...);

So, please change the return type of the methods in Command to this.

public command(name: string, desc?: string, opts?: commander.CommandOptions): this;

It seems that simply changing the definitions in index.d.ts is not enough. The main source code must also be changed.

See:
microsoft/TypeScript#4910

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions