microsoft / microsoft/TypeScript
Lazier typeof operator to enable limited "circular" references
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza reproduciendo el ejemplo de TypeScript del issue y compáralo con la discusión relacionada en #25214. Investiga el comportamiento del comprobador de tipos para keyof typeof y los alias de tipo circulares; se considera terminado cuando el ejemplo compila, se conserva la inferencia correcta y se evitan regresiones.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100