2

我正在使用 oclif 构建一个 CLI 实用程序,用 Typescript 编写它。

在 vscode 内部,所有生成的命令文件都给我一个错误,上面写着

如果没有对 '../../../../../../Projects/bag-man/tmp/mynewcli/node_modules/@oclif/parser/ 的引用,则无法命名推断的“标志”类型库/标志”。这可能是不可移植的。类型注释是必要的。

更令人沮丧的部分是,在某些文件中,它针对同一实例重复错误 4 次(即整个文件中只有一个红色下划线,但“问题”视图列出了 4 次相同的问题。)

这是显示 4 次的文件之一 - 这正是 oclif 为命令文件生成的内容。问题出现在第 12 行flags

import {Command, flags} from '@oclif/command'

export default class Hello extends Command {
  static description = 'describe the command here'

  static examples = [
    `$ mynewcli hello
hello world from ./src/hello.ts!
`,
  ]

  static flags = {
    help: flags.help({char: 'h'}),
    // flag with a value (-n, --name=VALUE)
    name: flags.string({char: 'n', description: 'name to print'}),
    // flag with no value (-f, --force)
    force: flags.boolean({char: 'f'}),
  }

  static args = [{name: 'file'}]

  async run() {
    const {args, flags} = this.parse(Hello)

    const name = flags.name ?? 'world'
    this.log(`hello ${name} from ./src/commands/hello.ts`)
    if (args.file && flags.force) {
      this.log(`you input --force and --file: ${args.file}`)
    }
  }
}

关于我需要在代码或我的 vscode linter 设置中修改什么来解决这个问题的任何想法?

4

2 回答 2

2

我通过将类型添加到flags字段来解决这个问题。例如:

static flags : flags.Input<any> = {
  // your flags...
}
于 2021-11-08T22:43:04.413 回答
1

我没有调查原因,但"declaration": false在你的设置中tsconfig.json解决了这个问题。

于 2021-09-07T09:19:08.193 回答