Skip to content
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

Type inference for async generators #44264

Open
5 tasks done
dimaqq opened this issue May 26, 2021 · 3 comments
Open
5 tasks done

Type inference for async generators #44264

dimaqq opened this issue May 26, 2021 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@dimaqq
Copy link

dimaqq commented May 26, 2021

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:

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 26, 2021

This sounds like use-site based inference, which is discussed more generally over at #15114. It would be awfully strange for us to infer the types of iterable values, async values, or especially just async iterable variables, but not others.

@dimaqq
Copy link
Author

dimaqq commented May 26, 2021

☝🏿 that is an old ticket that is closed and locked...

I think that partial type inference (an async thing; or an async generator producing any values) could also be useful...

@DanielRosenwasser
Copy link
Member

that is an old ticket that is closed and locked...

Right, what I mean is that it's very unlikely that we will be pursuing that direction, and many of the same issues would be relevant here. Even if we could solve it for a subset, it would be a pretty inconsistent ruleset of inference.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels May 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants