Here is an example of code that compiles successfully
import yargs from "yargs";
const parser = yargs(process.argv.slice(2)).
usage("$0 [filename]").
demandCommand(1);
async function main() {
const argv = await parser.argv;
}
And here is an example where the code does not compile
import yargs from "yargs";
const parser = yargs(process.argv.slice(2)).
usage("$0 [filename]").
demandCommand(1);
parser.argv.then(argv => {});
This results in compile errors:
src/index.ts:13:1 - error TS18046: 'parser.argv.then' is of type 'unknown'.
13 parser.argv.then(argv => {});
~~~~~~~~~~~~~~~~
src/index.ts:13:18 - error TS7006: Parameter 'argv' implicitly has an 'any' type.
13 parser.argv.then(argv => {});
~~~~
Based on my language server, the type of parser.argv
is as follows:
(property) yargs.Argv<{}>.argv: {
[x: string]: unknown;
_: (string | number)[];
$0: string;
} | Promise<{
[x: string]: unknown;
_: (string | number)[];
$0: string;
}>
I initially assumed that using await
on a promise object and using .then
on a promise object would yield the same result. However, it seems they behave differently in Typescript. Can you explain why?