我正在使用 ocif 构建一个 CLI 实用程序,并用 Typescript 编写。
在 vscode 中,所有生成的命令文件都给我一个错误,提示
The inferred type of 'flags' cannot be named without a reference to '../../../../../../Projects/bag-man/tmp/mynewcli/node_modules/@oclif/parser/lib/flags'. This is likely not portable. A type annotation is necessary.
额外令人沮丧的部分是,在某些文件中,它对同一实例重复错误 4 次(即整个文件中只有一条红色下划线,但“问题” View 列出了相同的问题 4 次。)
这是显示它 4 次的文件之一 - 这正是 ocif 为命令文件生成的内容。该问题出现在 flags
的第 12 行。
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 设置中修改什么来解决这个问题吗?
最佳答案
我还没有研究原因,但在 tsconfig.json
中设置 "declaration": false
解决了这个问题。
关于typescript - 在vscode中使用oclif,如何解决 "The inferred type of ' flags'无法在没有引用的情况下命名...”问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68914671/