Я пытаюсь использовать командующий в машинописном тексте, и я хотел бы дать правильный тип своему кли. Итак, я начинаю с этого кода:
import * as program from "commander";
const cli = program
.version("1.0.0")
.usage("[options]")
.option("-d, --debug", "activate more debug messages. Can be set by env var DEBUG.", false)
.parse(process.argv);
console.log(cli.debug)
Но я получаю эту ошибку:
example.ts(9,17): error TS2339: Property 'debug' does not exist on type 'Command'.
Поэтому я попытался добавить интерфейс, как описано здесь:
import * as program from "commander";
interface InterfaceCLI extends commander.Command {
debug?: boolean;
}
const cli: InterfaceCLI = program
.version("1.0.0")
.usage("[options]")
.option("-d, --debug", "activate more debug messages. Can be set by env var DEBUG.", false)
.parse(process.argv);
console.log(cli.debug)
и я получаю эту ошибку:
example.ts(3,32): error TS2503: Cannot find namespace 'commander'.
Насколько я понимаю, cli
на самом деле является классом типа commander.Command
Поэтому я попытался добавить класс:
import * as program from "commander";
class Cli extends program.Command {
public debug: boolean;
}
const cli: Cli = program
.version("1.0.0")
.usage("[options]")
.option("-d, --debug", "activate more debug messages. Can be set by env var DEBUG.", false)
.parse(process.argv);
console.log(cli.debug)
Что дает мне эту ошибку:
example.ts(7,7): error TS2322: Type 'Command' is not assignable to type 'Cli'.
Property 'debug' is missing in type 'Command'.
Я не знаю, как добавить свойство в класс Command
ни в свой файл, ни в новый файл .d.ts.