Support cyclical type inference of self-referential data structures, when subject to some assignability constraints #61659
Labels
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
π Search Terms
cyclical, recursive, inference, self-referential, extends, schema, zod
I'm trying to get recursive schemas working in Zod, powered by TypeScript's ability to handle self-reference inside getters. Example:
This would be a massive usability win for Zod and other schema/ORM libraries. None of these are able to directly represent cyclical types currently without redundant type annotations or some "scope"/"registry" concept to defer type resolution and later perform some kind of recursive substitution.
My understanding is that TypeScript is generally capable of inferring a recursive type with it's
...
approach as long as there aren't any assignability constraints on the data being inferred.Applying any constraint breaks this capability. But in the case below, it seems potentially possible to typecheck the cyclical type against the constraint without issue.
I think if the compiler was able to properly infer
a
above despite thesatisfies
clause, Zod could then support recursive objects in the way I'd described.β Viability Checklist
β Suggestion
Support inference on self-referential/cyclical data structures in more cases
π Motivating Example
Below is a more motivated example that I think demonstrates the same fundamental limitation I referenced earlier.
π» Use Cases
To support more ergonomic representations of inherently cyclical data structures (Zod schemas, database schemas, state diagrams, etc)
Not possible
None/not possible
The text was updated successfully, but these errors were encountered: