question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript example doesn't compile

See original GitHub issue

Hello,

I’m trying to use yargs with typescript but receive errors even after copying code from the docs.

The interesting part is that it starts to work after I await for result of argv.

image

Here is the text of error:

Element implicitly has an 'any' type because expression of type '"a"' can't be used to index type '{ [x: string]: unknown; a: boolean; b: string; c: number | undefined; d: (string | number)[] | undefined; e: number; f: string | undefined; _: (string | number)[]; $0: string; } | Promise<{ [x: string]: unknown; a: boolean; b: string; c: number | undefined; ... 4 more ...; $0: string; }>'.
  Property 'a' does not exist on type '{ [x: string]: unknown; a: boolean; b: string; c: number | undefined; d: (string | number)[] | undefined; e: number; f: string | undefined; _: (string | number)[]; $0: string; } | Promise<{ [x: string]: unknown; a: boolean; b: string; c: number | undefined; ... 4 more ...; $0: string; }>'

Here is my tsconfig:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "outDir": "dist",
    "lib": ["ESNext"]
  },
  "include": ["./src/**/*"]
}

I’m using:

"@types/yargs": "^17.0.0",
"yargs": "^17.0.1"

Thanks in advance

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
kitesicommented, Jun 1, 2021

As you can see from the error, the type of argv is a union. The first item is the arguments and the second one is a promise which resolves to those arguments. That is why when you await it, it gets the correct type.

The reason it’s a union is because in yargs you can have commands, and the handlers for those commands can be asynchronous. And so .argv would resolve after that command finishes.

In the example, you aren’t using any asynchronous commands, but yargs typing doesn’t know that when you are just calling .argv or .parse.

What you need to do is use #parseSync which explicitally tells yargs you have no asynchronous commands (this will also throw errors if there are async commands).

#!/usr/bin/env node
import yargs from 'yargs/yargs';

const argv = yargs(process.argv.slice(2)).options({
  a: { type: 'boolean', default: false },
  b: { type: 'string', demandOption: true },
  c: { type: 'number', alias: 'chill' },
  d: { type: 'array' },
  e: { type: 'count' },
  f: { choices: ['1', '2', '3'] }
}).parseSync();

argv.b // no error

code

6reactions
mafo5commented, Jun 4, 2021

Thanks. Exactly what I am looking for. Should be in the documentation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Compiling with Visual Studio Code
Compiling TypeScript. TypeScript is a typed superset of JavaScript that transpiles to plain JavaScript. It offers classes, modules, and interfaces to help ...
Read more >
Why doesn't this example compile? - Stack Overflow
1 Answer 1 ... That just has the implementation omitted. ... Or declare to tell Typescript you only want to pretend that this...
Read more >
TS does not compile .ts files #32326 - microsoft/TypeScript
Typescript compiles my file and creates sample.js. Actual behavior: Nothing happens. .js version is not created.
Read more >
TypeScript - Parcel
TypeScript is a typed superset of JavaScript that compiles to JavaScript. Parcel supports TypeScript out of the box without any additional configuration.
Read more >
Compile TypeScript Project - TutorialsTeacher
Compile TypeScript Project. Here, you will learn how to compile a TypeScript project and also learn about tsconfig.json. As you know, TypeScript files...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found