Replies: 1 comment 1 reply
-
|
Hi @abarani! I'm Dosu and I’m helping the zod team. TypeScript's structural typing means that two types are considered compatible if their shapes match, even if optional/required properties differ. This is why Zod's generics and the If you need strict enforcement (including optional/required), you can use a helper type to compare the inferred input type of your schema to your desired type, for example: type StrictMatch<A, B> =
(<T>() => T extends A ? 1 : 2) extends
(<T>() => T extends B ? 1 : 2) ? true : false;
type ExpectStrict<T extends true> = T;
// Usage:
type SchemaInput = { title?: string | undefined };
type Schema3Input = z.input<typeof schema3>;
type IsStrict = StrictMatch<Schema3Input, SchemaInput>;
type _Check = ExpectStrict<IsStrict>; // Error if not strictly equalThis will error if the types are not strictly equal, including optionality. You can use this pattern in your tests or as a type assertion. Let me know if this answers your question—if so, feel free to close the issue! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to use
ZodTypeto enforce that a schema matches a specific input type.Take this sample code:
In this case
schema3does not error, even though title is not optional in the schema implementation.Is there a way to strictly enforce, at the type level, that a Zod schema strictly matches (including optional/required in this case) a specific input type?
Any advice or examples would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions