You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I'm trying to use z.union together with multiple arrays that would let's say come from different files.
Below is the example of my code, variable names are just for the purpose of the example.
import{z}from'zod';constarray1=[z.object({type: z.literal('TEST_1'),}),];constarray2=[z.object({type: z.literal('SOME_OTHER_ACTION'),payload: z.object({myNumber: z.number(),})}),z.object({type: z.literal('NOTHING'),payload: z.object({otherNumber: z.number(),})}),z.object({type: z.literal('HELLO'),payload: z.object({firstNumber: z.number(),secondNumber: z.number(),})}),];/* The below results in error (replaced ZodObject with ...): Argument of type '...' is not assignable to parameter of type '[ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]'. Property '0' is optional in type '...' but required in type '[ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]'.ts(2345)*/constwsMessagesParser=z.union([
...array1,
...array2,]);
I guess this is to force users to provide at least 2 ZodTypeAny.
So for example doing something like this fixes the error mentioned above (or just anything that matches ZodTypeAny):
Argument of type '(ZodObject<{ type: ZodLiteral<"GET_SERVER_VERSION">; }, "strip", ZodTypeAny, { type?: "GET_SERVER_VERSION"; }, { type?: "GET_SERVER_VERSION"; }> | ... 15 more ... | ZodObject<...>)[]' is not assignable to parameter of type '[ZodDiscriminatedUnionOption<"type", Primitive>, ZodDiscriminatedUnionOption<"type", Primitive>, ...ZodDiscriminatedUnionOption<...>[]]'.
Source provides no match for required element at position 0 in target.ts(2345)
Looks like it's having a problem with the spread and thinking that there might be 0 elements? 🤔
Ahh, yeah the inferred type from TypeScript is a regular array of types (which is a super-set of 0-tuples and 1-tuples), but the type of ZodUnion requires that there are more than 1 element. You either need to declare your arrays with a tuple type that guarantees that spreading them includes at least two elements (which is hard since arrays are runtime mutable).
Personally, I would just declare those base arrays as unions themselves, but that doesn't work for your 1-tuple (array1 in your example), but if your normal use-case typically includes arrays of length > 1, you can do that:
Thanks @scotttrinh, it looks like it's working fine. As for arrays of size 1 I can just still use the spread operator, but place them at the bottom of the final union and it works fine.
So simply if the first 2 arrays are used like you mention and both have a length of >=2 then the rest can be a spread operator.
This discussion was converted from issue #817 on February 27, 2022 14:12.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I'm trying to use z.union together with multiple arrays that would let's say come from different files.
Below is the example of my code, variable names are just for the purpose of the example.
I guess this is to force users to provide at least 2 ZodTypeAny.
So for example doing something like this fixes the error mentioned above (or just anything that matches ZodTypeAny):
Does anyone maybe have some proper way around this without the need of requiring those 2 dummies?
Beta Was this translation helpful? Give feedback.
All reactions