microsoft / microsoft/TypeScript
Type inference for async generators
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio riportato di async generator e verifica la diagnostica implicit-any corrente per forever(test()). Non sono indicati file, test o punti di ingresso; esamina le problematiche correlate in #36687, #36967 e #32523. Il lavoro sarà completato quando saranno definiti l’ambito e il comportamento di inferenza previsto per async iterators e generators.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100