Hey people I am having some issue not
Hey people, I am having some issue, not sure how to resolve. Recursive types.
When building:
11 Replies
cc @Scott Trinh
I think you'll need to split up
CheckGroupSchema
to a "base" and then extend it with the lazy schema, like in the example on zod.dev
If you can throw this in a TypeScript playground, that'll be helpful for anyone wanting to provide some helpTS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
let me see If I can split
Importantly, to make the type recursive, you're required to provide the type of the final schema explicitly.
You can define a recursive schema in Zod, but because of a limitation of TypeScript, their type can't be statically inferred. Instead you'll need to define the type definition manually, and provide it to Zod as a "type hint"
Yeah When I do it more erros arrise unfortunately
share your updates
(type errors are helpful and usually lead to the solution!)
I suspect it is in the discriminatedUnion
one sec, let me debug
both
.default()
and .discriminatedUnion()
don't play well with lazy
wait, that's because the types are input types
It doesn't seem to bring me any errors
@Scott Trinh Thanks, it works 😄
cc @Scott Trinh Encountered this issue again today. Is there a reason why .discriminatedUnion()
with .lazy()
doesn't seem to play nice?
As soon as my object ofcourse get's wrapped by the lazy, it's not compatible anymore with type ZodDiscriminatedUnionOption<'type'>
(where 'type'
is my key for the discriminated union)discriminatedUnion
is a not-very-great optimization on union
, so you might have to go with union
to make it work.Yeahh i feared that
But it does make error messaging a lot more precise to my experience
oh, yeah, 100%. luckily, zod v4 will get us into a better place here where
union
will act like a discriminated union.