microsoft / microsoft/TypeScript
Change infer keyword behaviour to use it for type caching
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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
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 riproducendo gli esempi di VERY_HEAVY_COMPUTATION, MakePair e Result dell'issue e confronta il comportamento attuale di A extends infer Cache ? ... : .... Determina come dovrebbe funzionare il caching dei tipi senza richiedere il controllo condizionale ripetuto; il lavoro è completato quando la forma proposta di Result supera il controllo dei tipi preservando il tipo inferito previsto.
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
- 25/100