typescriptvisual-studio-codeoclif

Using oclif in vscode, how to resolve "The inferred type of 'flags' cannot be named without a reference to..." problem?


I am building a CLI utility using oclif, writing it in Typescript.

Inside vscode all of the generated command files are giving me an error that says

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.

The extra frustrating part is that in some files it repeats the error 4 times for the same instance (ie there is only a single red underline in the whole file, but the "Problems" view lists the same problem 4 times.)

This is one of the files which shows it 4 times - this is exactly what oclif generates for a command file. The problem is found on line 12 on 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}`)
    }
  }
}

Any idea on what I need to modify, either in the code or in my vscode linter settings, to resolve this?


Solution

  • I have not looked in to why, but setting "declaration": false in your tsconfig.json fixes the issue.