Open FlynnHillier opened 7 months ago
This is a good idea; one-element and even zero-element unions have reasonable conventional semantics, and can probably be easily implemented in the type system with conditionals!
My goal was for ZodUnion
to have a strongly typed tuple containing the element schemas. The current type signature is designed to prevent things like your TILES.map()
call, because then you can't do something like this:
myUnion.options[2];
// should be ZodLiteral<"a3">
I'm looking into loosening this restriction in the next major version. This is a recurring issue for people, and it doesn't impact the inferred type so it's probably not worth the trouble.
I am attempting to use a dynamically generated array of strings to define a
z.union()
that validates that a string is included within said dynamic array.To do this I am mapping each element of my dynamic array of strings to
z.literal()
, and then spreading this mapped array of literals to the union type.This implementation results in typescript kicking up a fuss, as it is not sure that union has been provided any values. This is because zod's
union
type is hardcoded to have atleast 2 ZodTypes provided to it.This means to prevent typescript from complaining, 2 literals must be hardcoded before we spread our array of literals into the union.
This is evidently not ideal as we may not have two values which we always wish to be included in our union type.
Is there any way
z.union()
could be re-typed / changed in some way to better support spreading of ZodType arrays?