Skip to content

"'await' has no effect on the type of this expression" from inferred void and Promise<void> union #55456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joshkel opened this issue Aug 21, 2023 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@joshkel
Copy link

joshkel commented Aug 21, 2023

🔎 Search Terms

"'await' has no effect on the type of this expression" , "promise<void>"

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.1-rc#code/MYewdgzgLgBGIgA4wLwwBQEpUD4YG8BfAbgChSBDCATzGBgDMBXOqAS3EYXQYBsKA5gC4YAIwS8AphTDZ8pGDFCRYkgB6TgTKJNSN+AmAH4YVWvSy4CMAPQAqGABMQMdYkmQ2AN10B3EABOANYwdjYwhDAi8EhkihS+FGyqGlo6WGSEpEA

💻 Code

const noop = () => {};

async function foo(flag: boolean) {
  const execute = flag ? async () => { /* do expensive work */ } : noop;
  await execute();
}

🙁 Actual behavior

The type of execute is inferred as () => void, so TypeScript displays a spurious warning on the await execute(); line, "'await' has no effect on the type of this expression.(80007)".

🙂 Expected behavior

TypeScript infers the type as () => void | () => Promise<void>, and calling await execute() generates no warnings.

Based on the FAQ, I would expect explicitly assigning () => Promise<void> to () => void to work, but I would not expect () => void | () => Promise<void> to be inferred as () => void and correct code then generating compiler diagnostics.

Additional information about the issue

I found some possibly related issues: #44201, #43921, #49755

If this issue is actually a duplicate of one of those, I apologize.

@fatcerberus
Copy link

fatcerberus commented Aug 22, 2023

I believe this is unfortunate fallout of subtype reduction.

@RyanCavanaugh
Copy link
Member

Yeah, there's not terribly much we can do here. The best solution is to annotate noop:

const noop = (): undefined => {};

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants