“Promise对象通过await/then返回不同类型的结果”

huangapple go评论62阅读模式
英文:

Promise object results in different type with await/then

问题

This code compiles

import yargs from "yargs";

const parser = yargs(process.argv.slice(2)).
    usage("$0 [filename]").
    demandCommand(1);

async function main() {
    const argv = await parser.argv;
}

while this does not

import yargs from "yargs";

const parser = yargs(process.argv.slice(2)).
    usage("$0 [filename]").
    demandCommand(1);

parser.argv.then(argv => {});

with 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 => {});
                    ~~~~

My language server reports that type of parser.argv is

(property) yargs.Argv<{}>.argv: {
    [x: string]: unknown;
    _: (string | number)[];
    $0: string;
} | Promise<{
    [x: string]: unknown;
    _: (string | number)[];
    $0: string;
}>;

I thought awaiting on promise object and .thening on promise object would result in same type but it did not. Does await/then behave in different ways in Typescript?

英文:

This code compiles

import yargs from &quot;yargs&quot;;

const parser = yargs(process.argv.slice(2)).
    usage(&quot;$0 [filename]&quot;).
    demandCommand(1);

async function main() {
    const argv = await parser.argv;
}

while this does not

import yargs from &quot;yargs&quot;;

const parser = yargs(process.argv.slice(2)).
    usage(&quot;$0 [filename]&quot;).
    demandCommand(1);

parser.argv.then(argv =&gt; {});

with compile errors

src/index.ts:13:1 - error TS18046: &#39;parser.argv.then&#39; is of type &#39;unknown&#39;.

13 parser.argv.then(argv =&gt; {});
   ~~~~~~~~~~~~~~~~

src/index.ts:13:18 - error TS7006: Parameter &#39;argv&#39; implicitly has an &#39;any&#39; type.

13 parser.argv.then(argv =&gt; {});
                    ~~~~

My language server reports that type of parser.argv is

(property) yargs.Argv&lt;{}&gt;.argv: {
    [x: string]: unknown;
    _: (string | number)[];
    $0: string;
} | Promise&lt;{
    [x: string]: unknown;
    _: (string | number)[];
    $0: string;
}&gt;

I thought awaiting on promise object and .thening on promise object would result in same type but it did not. Does await/then behave in different ways in Typescript?

答案1

得分: 1

"Seems that parser.argv is not a promise at runtime.

While you can await any value, you can only call .then() on a promise (or then-able, aka an object with a then method).

If you want to ensure a promise value, you can wrap it with Promise.resolve()

Promise.resolve(parser.argv).then((argv) =&gt; {
  // ...
});
英文:

Seems that parser.argv is not a promise at runtime.

While you can await any value, you can only call .then() on a promise (or then-able, aka an object with a then method).

If you want to ensure a promise value, you can wrap it with Promise.resolve()

Promise.resolve(parser.argv).then((argv) =&gt; {
  // ...
});

huangapple
  • 本文由 发表于 2023年6月29日 10:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定