microsoft / microsoft/TypeScript
Lazier typeof operator to enable limited "circular" references
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire l’exemple TypeScript de l’issue et comparez-le avec la discussion associée dans #25214. Étudiez le comportement du vérificateur de types pour keyof typeof et les alias de types circulaires ; le travail est terminé lorsque l’exemple se compile tout en préservant l’inférence correcte et en évitant les régressions.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100