DeferredShape interface

Deferred reference to a shape value. The thunk is called on dereference so the actual shape lookup is deferred.

Why this exists instead of embedding the shape directly.

Model shapes frequently reference each other — a model's properties can point to other models, and ARM types include both self- referential (tree-like) and mutually-referential (cyclic) structures. If we embedded a ModelShape value directly inside another model shape's value field, both shapes would need to be fully initialized when the parent is constructed. That's impossible whenever:

  • A model references itself, e.g. TreeNode { children: TreeNode[] }.
  • Two models reference each other, e.g. A { b?: B } and B { a?: A }.
  • Two modules each define shapes that reference shapes from the other.

In all three cases the second constant is in the Temporal Dead Zone when the first one's literal evaluates, and the runtime throws ReferenceError: Cannot access ... before initialization.

Wrapping the reference in a thunk solves it. The closure captures the binding name, not its current value. The lookup happens later when the thunk is called — by which time the module graph has finished evaluating and both shapes are fully constructed.

Only the model variant of ValueShape carries a reference — container shapes (array, record) are eager because they don't reference any module-level binding directly; the inner reference deeper down handles cycle-breaking.

Properties

kind
value

Property Details

kind

kind: "deferred"

Property Value

"deferred"

value

value: () => T

Property Value

() => T