Title: Recursive inference through self-referential object literals · Issue #64192 · microsoft/TypeScript · GitHub
Open Graph Title: Recursive inference through self-referential object literals · Issue #64192 · microsoft/TypeScript
X Title: Recursive inference through self-referential object literals · Issue #64192 · microsoft/TypeScript
Description: A library that derives static types from a runtime description of data has to let a schema reference itself, or it can't describe recursive data. The only place to put that self-reference is a getter, because category has no type until i...
Open Graph Description: A library that derives static types from a runtime description of data has to let a schema reference itself, or it can't describe recursive data. The only place to put that self-reference is a gett...
X Description: A library that derives static types from a runtime description of data has to let a schema reference itself, or it can't describe recursive data. The only place to put that self-reference is a ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/64192
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Recursive inference through self-referential object literals","articleBody":"A library that derives static types from a runtime description of data has to let a schema reference itself, or it can't describe recursive data. The only place to put that self-reference is a getter, because `category` has no type until its own declaration finishes.\n\n```ts\nconst category = object({\n name: string(),\n get subcategories() {\n return array(category);\n },\n});\n```\n\nThat doesn't infer. Both declarations collapse to `any`:\n\n```\nerror TS7022: 'category' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.\nerror TS7023: 'subcategories' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.\n```\n\nThe annotation the error asks for is the type that inference was supposed to produce, so there is nothing the author can write in its place.\n\n## The workaround\n\n[colinhacks/recursive-inference](https://github.com/colinhacks/recursive-inference) has two copies of a small schema library. One is written the normal way and one is written the way Zod actually does it. Both declare the same five recursive schemas.\n\nThe normal version, `idiomatic.ts`, takes output and input as ordinary type parameters, constrains every parameter against the real base interface, and reads the output type back with an indexed access. It produces 11 of the errors above on TypeScript 7, and the same 11 on 5.9.3 and 5.5.4.\n\nThe other version, `shipped.ts`, has the workarounds Zod ships. It compiles. The constraint is replaced with a structural type that doesn't mention the parameters, and the output type is read through a conditional type so the lookup is deferred.\n\n```ts\n// idiomatic: the constraint says what a schema is, and the read is direct\nexport type output\u003cT extends $ZodType> = T[\"_zod\"][\"output\"];\nexport interface $ZodArray\u003cT extends $ZodType = $ZodType> extends $ZodType\u003coutput\u003cT>[], input\u003cT>[]> {}\n\n// what ships: the constraint discards the parameters, and the read defers\nexport type SomeType = { _zod: _$ZodTypeInternals };\nexport type output\u003cT> = T extends { _zod: { output: any } } ? T[\"_zod\"][\"output\"] : unknown;\nexport interface $ZodArray\u003cT extends SomeType = $ZodType> extends $ZodType {\n _zod: $ZodArrayInternals\u003cT>;\n}\n```\n\nZod does this at 296 sites so that recursive schemas infer for users. The `SomeType` constraint accepts anything with the right shape of internals, so at those sites Zod can't constrain a parameter to be a schema.\n\n## Other libraries\n\nRecursive data is common: category trees, comment threads, filesystem nodes, self-referencing foreign keys, state machines whose transitions name other states, agents whose handoffs name other agents. Any library that derives a static type from a runtime description of that data hits the same problem.\n\nZod absorbs the cost inside the library, at those 296 sites, so users don't see it.\n\nDrizzle puts it on the user. A self-referencing foreign key needs a hand-written return type:\n\n```ts\nreportsTo: integer('reports_to').references((): AnyPgColumn => employees.id),\n```\n\nWithout the annotation the arrow's return type is inferred from `employees`, which is still being declared, and that is the same failure. The annotation appears throughout Drizzle's own tests and docs. The `AnyPgColumn` type is the same kind of widened stand-in as Zod's `SomeType`. It means some column of some table, because the real column type can't be named yet.\n\nNeither cost shows up as a bug report. From the outside both features work; Zod infers recursive schemas and Drizzle links self-referencing tables.\n\n## Scope\n\nI don't know how big the general problem is. [#64172](https://github.com/microsoft/TypeScript/pull/64172) fixes the case above, and with it all 296 of Zod's substitutions can be deleted. It has tests and measurements, but it is a fix for one shape, not a general design. A design that covers more of this would be better than the specific fix.\n","author":{"url":"https://github.com/colinhacks","@type":"Person","name":"colinhacks"},"datePublished":"2026-09-07T19:13:24Z","url":"https://github.com/microsoft/TypeScript/issues/64192"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d0c5d5f8-b787-127b-58a1-3f739981b7a3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BFC8:DA744:AC7615:E8C148:6B00F4FC |
| html-safe-nonce | bfb63099986fa55b589db9c5aaa81b6aa3ed96d036ef0d1e4541ef6acfaa6f5b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRkM4OkRBNzQ0OkFDNzYxNTpFOEMxNDg6NkIwMEY0RkMiLCJ2aXNpdG9yX2lkIjoiNjYzNTIzNzMzMDMzNDU3NzkxNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1c58526f1d13ab847368e6183244b326781631f02bc280bdeb94de2be4586420 |
| hovercard-subject-tag | issue:5378261925 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/64192/issue_layout |
| twitter:image | https://opengraph.githubassets.com/15b35306123211d10029c9e407670c766bdd3681557e545e19d1b5ae9472a3c8/microsoft/TypeScript/issues/64192 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/15b35306123211d10029c9e407670c766bdd3681557e545e19d1b5ae9472a3c8/microsoft/TypeScript/issues/64192 |
| og:image:alt | A library that derives static types from a runtime description of data has to let a schema reference itself, or it can't describe recursive data. The only place to put that self-reference is a gett... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | colinhacks |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width