Skip to content

Async generator functions plus generics cause confusing errors about Awaited on return #48966

@kristian-elder

Description

@kristian-elder

Bug Report

🔎 Search Terms

"asyncgenerator"
"async generator awaited"

🕗 Version & Regression Information

When checking the Typescript playground, the behaviour exists in 4.5.5 but not 4.4.4 and is still present in Nightly as of 5 May 2022.

⏯ Playground Link

Playground link with relevant code

💻 Code

async function* test<T, TReturn, TNext>(): AsyncGenerator<T, TReturn, TNext> {
  // const foo: Awaited<TReturn> = null as any // this would one be accepted as the return type in more recent typescript
  const bar: TReturn = null as any // Although this matches TReturn exactly, it is not accepted as a return value
  return bar
}

🙁 Actual behavior

Typescript rejects a return from an async generator that matches the return type argument from a generic AsyncGenerator, instead expecting an Awaited. This is confusing when creating generic helper methods dealing with generators, and also unexpected as presumably an async generator should be allowed to return both a T and Promise.

🙂 Expected behavior

Returning with a value matching the generic return type from AsyncGenerator should be accepted as value.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions