Skip to content
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

Allow creation of discriminated unions with a readonly array of options #3535

Merged

Conversation

haines
Copy link
Contributor

@haines haines commented May 29, 2024

If I extract the array of options out of the z.discriminatedUnion call, type-checking fails because an array is not assignable to a non-empty tuple:

const options = [
  z.object({ type: z.literal("x"), val: z.literal(1) }),
  z.object({ type: z.literal("y"), val: z.literal(2) }),
];

const schema = z.discriminatedUnion("type", options);
TS2345: Argument of type '(ZodObject<{ type: ZodLiteral<"x">; val: ZodLiteral<1>; }, "strip", ZodTypeAny, { type: "x"; val: 1; }, { type: "x"; val: 1; }> | ZodObject<{ type: ZodLiteral<...>; val: ZodLiteral<...>; }, "strip", ZodTypeAny, { ...; }, { ...; }>)[]' is not assignable to parameter of type '[ZodDiscriminatedUnionOption<"type">, ...ZodDiscriminatedUnionOption<"type">[]]'.
  Source provides no match for required element at position 0 in target.

However, if I add as const to the options definition to make it a readonly tuple, now type-checking fails because the options are not mutable:

TS2345: Argument of type 'readonly [ZodObject<{ type: ZodLiteral<"x">; val: ZodLiteral<1>; }, "strip", ZodTypeAny, { type: "x"; val: 1; }, { type: "x"; val: 1; }>, ZodObject<{ type: ZodLiteral<...>; val: ZodLiteral<...>; }, "strip", ZodTypeAny, { ...; }, { ...; }>]' is not assignable to parameter of type '[ZodDiscriminatedUnionOption<"type">, ...ZodDiscriminatedUnionOption<"type">[]]'.
  The type 'readonly [ZodObject<{ type: ZodLiteral<"x">; val: ZodLiteral<1>; }, "strip", ZodTypeAny, { type: "x"; val: 1; }, { type: "x"; val: 1; }>, ZodObject<{ type: ZodLiteral<...>; val: ZodLiteral<...>; }, "strip", ZodTypeAny, { ...; }, { ...; }>]' is 'readonly' and cannot be assigned to the mutable type '[ZodDiscriminatedUnionOption<"type">, ...ZodDiscriminatedUnionOption<"type">[]]'.

This PR makes it possible to do this with as const by making the generic constraint readonly.

Signed-off-by: Andrew Haines <andrew@haines.org.nz>
@haines haines force-pushed the readonly-discriminated-union-types branch from bf69980 to f8df082 Compare November 25, 2024 11:30
Copy link

netlify bot commented Nov 25, 2024

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f8df082
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/67445fc376857a00088e5976
😎 Deploy Preview https://deploy-preview-3535--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@haines
Copy link
Contributor Author

haines commented Nov 25, 2024

@colinhacks could you please take a look?

@colinhacks colinhacks merged commit 6407bed into colinhacks:main Dec 10, 2024
4 checks passed
@haines haines deleted the readonly-discriminated-union-types branch December 10, 2024 04:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants