microsoft / microsoft/TypeScript
Change infer keyword behaviour to use it for type caching
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
infer type cache
Suggestion
I saw one proposal somewhere, but I can't find it now. There people asked to add possibility of using type level variables inside type level computations. I realized now that simple constructions with infer keyword may be used for this after a small behaviour change:
type VERY_HEAVY_COMPUTATION<T> = T extends number ? 42 : []
type MakePair<P extends number, R extends number> = [P, R]
type Result = VERY_HEAVY_COMPUTATION<0> extends infer Cached
// Error: Type 'Cached' does not satisfy the constraint 'number'.ts(2344)
? MakePair<Cached, Cached> : never
Currently in this case VERY_HEAVY_COMPUTATION<0> doesn't share its value with Cached automatically, so we need compute the value twice.
type Result = VERY_HEAVY_COMPUTATION<0> extends infer Cached
? Cached extends VERY_HEAVY_COMPUTATION<0>
? MakePair<Cached, Cached> : never
: never
I suggest, in the case of such constuctions A extends infer Cache ? ... : ..., automatically share the type of A with Cache
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 damit, die Beispiele für VERY_HEAVY_COMPUTATION, MakePair und Result aus dem Issue nachzustellen und das aktuelle Verhalten von A extends infer Cache ? ... : ... zu vergleichen. Bestimme, wie Typ-Caching funktionieren sollte, ohne die wiederholte bedingte Prüfung zu erfordern; abgeschlossen ist die Aufgabe, wenn die vorgeschlagene Result-Form typgeprüft wird und dabei den beabsichtigten inferierten Typ bewahrt.
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
- 25/100