Skip to content

fix(v4): align record keys and intersection strictness with TypeScript - #6412

Merged
colinhacks merged 3 commits into
mainfrom
fix/intersection-unified
Aug 14, 2026
Merged

colinhacks merged 3 commits into
mainfrom
fix/intersection-unified

Conversation

@colinhacks

Copy link
Copy Markdown
Owner

Makes a record's key schema behave the way TypeScript treats an index signature, and stops an unrecognized key from suppressing the rest of a schema.

A record's key schema governs its own keys, not every key

z.object({ name: z.string() }).and(z.record(z.string().regex(/^S_/), z.string()))
  .parse({ name: "a", S_a: "s" });
// before: throws invalid_key on "name"
// now:    { name: "a", S_a: "s" }

name is the object's key, not the record's. TypeScript agrees β€” in {name: string} & Record<S_${string}, string> the index signature constrains only the keys matching it, while a matching key with the wrong value type is still an error. z.infer already matched TypeScript here; the parser did not, so the runtime rejected values the inferred type accepted.

This holds for every key-schema flavor: z.enum, z.literal([...]), z.string().regex(), z.templateLiteral(). Two changes get there. $ZodRecord chose its issue code from def.partial rather than from the key schema, so z.partialRecord(z.enum([...])) reported invalid_key where z.record(z.enum([...])) reported unrecognized_keys; it now classifies on the key schema. And handleIntersectionResults reconciles a record's invalid_key the same way it already reconciles unrecognized_keys, so a key one operand does not govern is reported only when no operand governs it.

The reconciliation rule itself is unchanged: report a key only when every operand objects to it. A stripping schema never objects, so it never contributes. Standalone records are unchanged β€” a key outside the key schema is still an error, matching TypeScript's excess-property check on a fresh object literal.

An unrecognized key no longer aborts the schema it came from

z.strictObject({ a: z.string() }).safeParse({ a: 1, extra: 2 });
// before: ["unrecognized_keys"]
// now:    ["invalid_type", "unrecognized_keys"]

unrecognized_keys describes the shape of the input, not the validity of the parsed value, so it is now continuable at every emission site. A strict operand inside an intersection runs its own checks, refinements and transforms; a .default() inside a nested intersection applies. The parse still fails.

A pipe keeps its rule that any issue stops it, so a failing refinement never feeds its transform and z.string().refine(isJSON).transform(JSON.parse) still fails cleanly rather than throwing. The single exception is unrecognized_keys, where the value handed downstream is identical whether or not the extra key was present.

Supersedes #5905, #6234 and #6248. Fixes #2200. Fixes #2573. Fixes #4017. Fixes #5663.

…enumerable

An enumerable key schema (z.enum, z.literal([...])) declares which keys the
record owns, so a key outside that set is unrecognized. A non-enumerable one
(regex, refine) is a constraint every key must satisfy, so a failing key is
invalid. Only the first is reconcilable against the other side of an
intersection, which handleIntersectionResults already does for
unrecognized_keys.

The branch that emitted the issue keyed off def.partial rather than the key
schema, so partialRecord(z.enum([...])) reported invalid_key and its keys were
never reconciled -- the object side of an intersection could not claim its own
keys. Fixes #2200, #2573.
A record's key schema declares which keys that record is responsible for. It
is not a predicate every key in the result must satisfy. TypeScript agrees:
in `{name: string} & Record<\`S_\${string}\`, string>` the index signature
constrains only the keys matching it, so `name` is accepted, while a matching
key with the wrong value type is still an error.

Zod's inferred types already match TypeScript here, but the runtime rejected
the object's own keys with invalid_key, so z.infer accepted values the parser
threw on. Two changes:

- partialRecord with an enumerable key schema now reports an out-of-set key as
  unrecognized_keys, like z.record(z.enum([...])) already did. The branch keyed
  off def.partial rather than off the key schema.
- handleIntersectionResults reconciles a record's invalid_key the same way it
  already reconciles unrecognized_keys, so a key one side does not govern is
  reported only when neither side governs it.

Standalone records are unchanged: a key outside the key schema is still an
error, matching TypeScript's excess-property check on a fresh literal.

Fixes #2200, #2573.
…logic

unrecognized_keys describes the shape of the input, not the validity of the
parsed value, so it no longer aborts the schema it came from. A strict operand
inside an intersection now runs its own checks, refinements and transforms, a
nested default still applies, and an extra key no longer suppresses the other
errors on the same object. The parse still fails.

A pipe keeps its rule that any issue stops it, so a failing refinement never
feeds its transform -- the one exception is unrecognized_keys, where the value
handed downstream is identical whether or not the extra key was present.
Copilot AI lite review requested due to automatic review settings August 14, 2026 05:35
@pullfrog

pullfrog Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

This run was cancelled πŸ›‘

The workflow was cancelled before completion. Please check the link below for details.

Pullfrog  | View workflow run | via Pullfrog | 𝕏

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@colinhacks
colinhacks merged commit 4e1720c into main Aug 14, 2026
7 of 8 checks passed
@colinhacks
colinhacks deleted the fix/intersection-unified branch August 14, 2026 05:52
colinhacks added a commit to colinhacks/frizz that referenced this pull request Aug 17, 2026
The queue showed an "Awaiting" card immediately above an "Awaiting background work" card,
both about the same rest. Maintainer 2026-08-13: "I keep seeing duplicates like this."

It is a seam between two correct changes. Since `pr-watch` stopped carrying a park action
(2026-08-13), an awaiting fence on a watched PR has nothing to offer, so it falls back to
a bare "Awaiting" heading β€” while the resting card below it states the same wait with the
counts and the only control. The heading was the whole of the duplication.

So a fence with NO park action, on a thread whose resting card is showing, drops its
chrome and keeps its content: the worker's own prose, rendered as the ordinary final
message it is. A fence with a real action β€” a `human` gate, a future `timer` β€” still earns
its card, because that control lives nowhere else. Guarded on `awaitingBackground` rather
than assumed: a thread parked on a watcher with no live shell or sub-agent shows no
resting card, and dropping the chrome there would leave the wait unstated.

The refs went the same way one level down. The watcher rows under the prompt box already
list every watched PR, so printing the fence's links too put `colinhacks/zod#6412` twice
on one card; they now render only when those rows are absent.

Verified against a rebuild of the reported shape β€” two `pr-watch` hints, a live background
shell, a resting thread: one statement of the wait, each PR named once.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
Preview – zod-v4 β€” 2e804edc Deployed Aug 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants