microsoft / microsoft/TypeScript

Type inference for async generators

Open
#44,264 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

It would be amazing if typescript could infer types from async generators.
The following is valid javascript, thus, I think, it should be valid typescript and type of x should be inferred.
Today it fails with Parameter 'x' implicitly has 'any' type.

async function forever(x) {
  for await (const unused of x) {
    void unused;
  }
}

async function* test() {
  for (;;) {
    await something();
    yield;
  }
}

forever(test());

🔍 Search Terms

async generator, async iterator, type inference, infer types

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Treat type of x as "any async iterator would do" thus including any async generator function.

📃 Motivating Example

My own use... forever/background (async) processes/

💻 Use Cases

Today, I need to type this explicitly, which is unnecessarily verbose.

In the long run, it would allow composing async functions same as type inference helps composing plain functions today.

I get it that type inference for async iterables/iterators/generators opens a huge can of worms, like:

  • type narrowing, like #36687 or in this vein
  • yield expression return type #36967
  • generator yielded type #32523

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported async generator example and verify the current implicit-any diagnostic for forever(test()). No files, tests, or entry points are named; review the related concerns in #36687, #36967, and #32523. Done requires a settled scope and expected inference behavior for async iterators and generators.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.