Unknown | Promise<...> should be a valid return type for IDE compatibility

  • What language is this for? Typescript

  • Which rule? typescript:S6571

  • Why do you believe it’s a false-positive/false-negative?
    While mostly true, accepting unknown | Promise<...> as a valid return type allows IDE’s to understand that the function is awaitable. Without including the Promise option, IDE’s will warn that the function doesn’t need to be awaited, when in fact it does.

  • Are you using

    • SonarCloud?
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

type Foo = (arg: number) => unknown | Promise<unknown>;
// 'unknown' overrides all other types in this union type

Hello @landisdesign,

thank you for the feedback. I would say that this is not an FP, as unknown could be as well Promise<unknown>. However, I understand that some IDEs may report the unneeded await. What IDE are you using? Intellij does not warn me and allows me to await it:

type Foo = (arg: number) => unknown;

async function bar(f: Foo) {
  await f(5); // Nothing reported by Intellij here
}

Cheers,
Victor

Hunh! I swore I saw it in VSCode, but now I cannot replicate it. :man_shrugging: I’ll go ahead and close this, and if I see it come up again in the wild I’ll be more careful to see what combinations of types might be causing it. Thanks for your patience in awaiting (heh) my response.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.