microsoft / microsoft/TypeScript

Type inference for async generators

Offen
#44,264 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem gemeldeten Beispiel für einen async generator und überprüfe die aktuelle implicit-any-Diagnose für forever(test()). Es werden keine Dateien, Tests oder Einstiegspunkte genannt; prüfe die damit verbundenen Anliegen in #36687, #36967 und #32523. Erledigt ist die Aufgabe, wenn der Umfang und das erwartete Inferenzverhalten für async iterators und generators festgelegt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.