-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
z.ZodType and z.ZodSchema do not seem to work as I expect #652
Comments
I believe this has something to do with structural typing in TypeScript. In the FWIW, trying to use Zod with existing types is always going to be a bit harder since it's specifically designed to be the source of truth for the types. Affordances to "check" schemas against existing types are prone to issues like the one you're encountering. If it's at all possible for you to turn this around and derive the types from your schemas, you'll have a bit better luck. |
Thank you for your answer @scotttrinh! I see... Sadly, this is a relevant limitation IMO. I think the domain of the application shouldn't be defined with a library.
I've updated the CodeSandbox adding a third case that demonstrates how Here the snippet: type C = {
foo: string;
bar?: string;
};
// This parser does not match the interface since bar is not defined as optional, and we get an error. Cool.
const CParser: t.Type<C> = t.type({
foo: t.string,
bar: t.string
});
// This parser does match the interface and we have no error. Cool.
const CParser2: t.Type<C> = t.intersection([
t.type({
foo: t.string
}),
t.partial({
bar: t.string
})
]); For my part, you can close this issue. Thanks again! |
Just for reference and in case it's useful for someone else facing these problems, we've ended up applying this solution, which works quite well for us since the compiler warns us whenever there's an inconsistency between the TS types and the Zod parsers. type Exact<T, U> = [T, U] extends [U, T] ? true : false;
const StrictSchema: <T>() => <U>(
u: Exact<ZodSchema<T>, ZodSchema<U>> extends true
? Exact<Required<T>, Required<U>> extends true
? ZodSchema<U>
: never
: never
) => ZodSchema<T> =
() =>
<T>(u: unknown) =>
u as ZodSchema<T>;
I've updated the CodeSandbox to demonstrate how it works (from line 38) and it seems to work in all the cases:
Credits should go to @yuhr, who put us on the right track. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi there!
I've discovered the lib recently and have a question that is probably silly.
I have an application that defines its domain with Typescript, which I want to be the single source of truth. This means I'm not interested in inferring static types from Zod, but the other way around. I want Zod to obey what Typescript dictates and want the TS compiler to let me know when any inconsistency arises. I assume the dual definition of types and parsers.
I've tried to achieve this with
z.ZodType<MyType>
andz.ZodSchema<MyType>
with no success and I have not found a clue to follow from the readme of the project, which seems to be the only documentation available.I've prepared a small dumb example that I hope will help you understand what I intend to do (CodeSandbox here):
I have some experience with
io-ts
. Please find here a CodeSandbox demonstrating the result I'd expect.Thx!!
The text was updated successfully, but these errors were encountered: