Repository navigation
feat(instanceof): key the .properties() shape off the instance type - #6600
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
✅ 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.
GPT Terra | 𝕏
1 task
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

The method took any
$ZodShape, soz.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 overkeyof 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 carryingURL's owntoStringentry 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.tsgrows. 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