microsoft / microsoft/TypeScript
Support conditional types for Generator T / TNext / TReturn
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔍 Search Terms
"Generator", "Async Generator", "conditional types", "const" "inference"
✅ Viability Checklist
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
- What do you want to use this for?
Complex state management and side effects.
- 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.
- 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>
})
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem motivierenden Generatorbeispiel und vergleiche die angeforderte bedingte Beziehung zwischen T, TNext und TReturn mit dem angegebenen aktuellen Verhalten für Generatoren und async Generatoren. Als erledigt gilt, wenn die bedingte Inferenz die beschriebenen typisierten yield-Ergebnisse unterstützt, ohne das emittierte JavaScript zu ändern, mit Abdeckung der motivierenden Fälle.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100