zod 4 toJSONSchema() throws a deferred SC2011 in finalize (Map entries spread)

Open
#3,605 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript
Domain
compilers

Research direction

Start by reproducing with the minimal index.ts (a z.object calling toJSONSchema()) and the scriptc build command from the issue, then read the failing line 295 of node_modules/zod/v4/core/to-json-schema.js, which spreads ctx.seen.entries() in finalize. The payload does not name the compiler source that emits SC2011, so the first step is locating where that diagnostic is raised for untyped Map entries. Done means the probe prints the expected draft 2020-12 JSON Schema without the SC2011 error.

Written by the indexing model from the issue text.

Description

toJSONSchema() from zod 4.4.3 compiles, but throws at run time with a deferred SC2011 inside zod's finalize.

index.ts:

import { z } from "zod";

console.log(JSON.stringify(z.object({ a: z.string() }).toJSONSchema()));

With [email protected] installed and a tsconfig.json of {"compilerOptions":{"target":"ES2022","module":"NodeNext","moduleResolution":"NodeNext","strict":true}}, scriptc build index.ts -o probe --optimization dev --node-islands=off builds (0.4.10 and 0.4.11). Running ./probe:

scriptc: not compiled: values of type 'any' have no native representation at this site (instantiating 'finalize' with ({ counter: number; cycles: unknown; external: unknown; io: unknown; metadataR...)) [SC2011 at node_modules/zod/v4/core/to-json-schema.js:295]
Uncaught Error: values of type 'any' have no native representation at this site (instantiating 'finalize' with ...) [SC2011 at node_modules/zod/v4/core/to-json-schema.js:295]
    at finalize (node_modules/zod/v4/core/to-json-schema.js:295:5)

Node prints {"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"a":{"type":"string"}},"required":["a"],"additionalProperties":false}.

Line 295 of to-json-schema.js is

for (const entry of [...ctx.seen.entries()].reverse()) {

where ctx.seen is a Map built in initializeContext (seen: new Map()), keyed by schema with untyped entry objects. The same happens through schema["~standard"].jsonSchema.input(...) and z.toJSONSchema(schema), which all reach finalize.

Programs that hand zod schemas to the MCP SDK (tool listings) or build structured-output prompts with z.toJSONSchema hit this.

Dominant language
TypeScript
Stars
6.7k
Forks
229
Avg merge
14h 29m
Merged PRs (30d)
787

Getting set up

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.

More from vercel-labs/scriptc

All issues in vercel-labs/scriptc

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.