microsoft / microsoft/TypeScript

Lazier typeof operator to enable limited "circular" references

Offen
#40,567 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das TypeScript-Beispiel im Issue zu reproduzieren, und vergleiche es mit der zugehörigen Diskussion in #25214. Untersuche das Verhalten des Type-Checkers für keyof typeof und zirkuläre Typ-Aliase; abgeschlossen bedeutet, dass das Beispiel kompiliert, während die korrekte Inferenz erhalten bleibt und Regressionen vermieden werden.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.