Typescript example doesn't compile
See original GitHub issueHello,
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.
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:
- Created 2 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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).
Thanks. Exactly what I am looking for. Should be in the documentation!