microsoft / microsoft/TypeScript

Lazier typeof operator to enable limited "circular" references

Open
#40,567 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the TypeScript example in the issue and compare it with the related discussion in #25214. Investigate the type-checker behavior for keyof typeof and circular type aliases; done means the example compiles while preserving correct inference and avoiding regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.