Skip to content

feat(instanceof): key the .properties() shape off the instance type - #6600

Merged
colinhacks merged 1 commit into
mainfrom
properties-shape-typesafety
Sep 13, 2026
Merged

colinhacks merged 1 commit into
mainfrom
properties-shape-typesafety

Conversation

@colinhacks

Copy link
Copy Markdown
Owner

The method took any $ZodShape, so z.instanceof(File).properties({ nmae: z.string() }) compiled, and so did a schema that can't accept the property's type. The shape is now a mapped type over keyof T, so the keys autocomplete and both of those are errors.

Methods are excluded from the shape, which was the one real obstacle here. Every object literal inherits Object.prototype.toString, so a constraint carrying URL's own toString entry rejects every shape literal passed to it.

Type-only — the emitted JS is byte-identical, so runtime, memory and bundle are all unchanged and only the .d.ts grows. It does tighten a public signature: a shape key outside the instance type, or on a method, now fails to compile where it previously didn't.

On the Zod Mini side: the spread form already rejects an unknown key and a wrong property type in both builds, since the check's input type demands the properties exist on the base. What it doesn't get is autocomplete, because the shape literal is written before any contextual type reaches it. That would need either a new exported helper, which costs bytes in every mini bundle, or a change to the shared .check() signature.

Refs #6598

The method took any $ZodShape, so a misspelled key or a schema that can't
accept the property's type both compiled. The shape is now a mapped type over
keyof T, which also makes the keys autocomplete.

Methods are excluded from the shape. Every object literal inherits
Object.prototype.toString, so a constraint carrying URL's own toString entry
rejects every shape literal passed to it.

Type-only: the emitted JS is byte-identical, so runtime, memory and bundle
are all unchanged.
Copilot AI lite review requested due to automatic review settings September 13, 2026 18:25

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.

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes Reviewed the initial type-level restriction on z.instanceof().properties() and its coverage.

  • Instance shape constraint The method now contextualizes property shapes from non-function keys of the instance type and checks schema input compatibility.
  • Type-level coverage The new assertions cover valid narrowing plus unknown keys, incompatible schemas, and method keys.

Pullfrog  | View workflow run | Using GPT Terra | 𝕏

@colinhacks
colinhacks merged commit 56222cd into main Sep 13, 2026
9 checks passed
@colinhacks
colinhacks deleted the properties-shape-typesafety branch September 13, 2026 18:30

This branch was successfully deployed

1 active deployment
Preview – zod-v4 — 5a5e67ed Deployed Sep 13, 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

Development

Successfully merging this pull request may close these issues.

2 participants