microsoft / microsoft/TypeScript

Lazier typeof operator to enable limited "circular" references

Aperta
#40,567 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

  • typeof referenced indirectly
  • lazy typeof
  • lazier typeof
  • circular typeof

#25214 is very closely related but not exactly the same.

Suggestion

If typeof operators were resolved slightly lazier, certain "circular" type definitions could be typed more automatically. Then the following could be made to compile:

type Place = keyof typeof building;
function room(doors: Place[]): {doors: Place[]} {
  return { doors };
}
const building = {
  foyer: room(["dining", "kitchen"]),
  dining: room(["foyer", "bedroom"]),
  kitchen: room(["foyer", "bedroom"]),
  bedroom: room(["dining", "kitchen"]),
};

Currently, you get

Type alias 'Place' circularly references itself.ts(2456)

However, Place doesn't actually reference itself since you don't need to know what Place is to evaluate keyof typeof building, only to evaluate typeof building in its entirety. If typeof building were resolved lazily (as it was needed) then its keys could be found without needing Place, breaking the reference cycle.

Specifically, we can resolve type Place = "foyer" | "dining" | "kitchen" | "bedroom" without needing to know what building's properties are specifically typed. Then, we can proceed with any inferences as needed for the building, getting the complete inferred type

type Place = "foyer" | "dining" | "kitchen" | "bedroom";
const building: Record<Place, {doors: Place[]}>; // or something equivalent, e.g. an actual object

Downsides/Tradeoffs

  • More complexity in type-checker
  • Relatively fringe use-case
  • Possibly brittle (small changes to variable definition or type definition may cause circularity to reappear, limiting possible use)

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 riproducendo l'esempio TypeScript nell'issue e confrontalo con la discussione correlata in #25214. Analizza il comportamento del type-checker per keyof typeof e gli alias di tipo circolari; il lavoro è completato quando l'esempio compila preservando l'inferenza corretta ed evitando regressioni.

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.