我尝试在打字稿中使用指挥官,我想给我的 cli 一个合适的类型。所以我从这段代码开始:
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 文件中向类添加属性。