microsoft / microsoft/TypeScript

Support conditional types for Generator T / TNext / TReturn

Aperta
#57,671 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

"Generator", "Async Generator", "conditional types", "const" "inference"

✅ Viability Checklist
⭐ Suggestion

If a generator function is specifically typed to have a conditional relationship between T and TNext then type inference should allow for a type narrowed result according to that conditional type.

📃 Motivating Example

Typescript would be able to support complex type state management (ala redux saga), coroutines (e.g. co) and the interpreter pattern / free monad/applicative.

In the past this was not possible, but given typescript now has as const, infer and conditional types, we should be able to support typed yield result inference in key circumstances.

Something like this would be possible:

type Yielded<T> 
  T extends Promise<infer U>
   ? U // awaited
   : T  // return the original yielded value
   
function co<T>( fn: () => Generator<T, T, Yielded<T>> ) : Promise<T> {...}
💻 Use Cases
  1. What do you want to use this for?

Complex state management and side effects.

  1. What shortcomings exist with current approaches?

For generator functions yielded values are always typed as any. For async generators yielded values are a union of all possible results.

  1. What workarounds are you using in the meantime?

Manually casting

doSomething(function * f(){
    let value = (yield someStream) as UnwrapStream<typeof someStream>
    let another = (yield somePromise) as UnwrapPromise<typeof somePromise>
})

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dall’esempio di generatore alla base della richiesta e confronta la relazione condizionale richiesta tra T, TNext e TReturn con il comportamento attuale dichiarato per i generatori e i generatori async. Il lavoro è completo quando l’inferenza condizionale supporta i risultati di yield tipizzati descritti senza modificare il JavaScript emesso, con la copertura dei casi motivanti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.