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

Add default types to createZodEnum #2690

Closed
wants to merge 1 commit into from

Conversation

mihirgokani007
Copy link

@mihirgokani007 mihirgokani007 commented Aug 28, 2023

Fixes #2691

When defining a zod enum from an existing union type, currently it is necessary to define it as such:

// Assuming AUnionType was defined somewhere as
type AUnionType = 1 | 2 | 3 | 4;

// If we want to provide explicit type to it, we have to define it like
z.enum<AUnionType, [AUnionType, AUnionType, AUnionType, AUnionType]>([1, 2, 3, 4]);

However, the explicit typing seems redundant. This commit just adds default types to enums, so now we can do:

z.enum<AUnionType>([1, 2, 3, 4]);

Same as before, we can provide explicit type when needed.

When defining a zod enum from an existing union type, currently it is necessary to define it as such:
```
// Assuming AUnionType was defined somewhere as
type AUnionType = 1 | 2 | 3 | 4;

// If we want to provide explicit type to it, we have to define it like
z.enum<AUnionType, [AUnionType, AUnionType, AUnionType, AUnionType]>([1, 2, 3, 4]);

```

However, the explicit typing seems redundant. This commit just adds default types to enums, so now we can do:
```
z.enum<AUnionType>([1, 2, 3, 4]);
```

Same as before, we can provide explicit type when needed.
@netlify
Copy link

netlify bot commented Aug 28, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit ab21cc0
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/64ecc4b16b7cfe0008932b24
😎 Deploy Preview https://deploy-preview-2690--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.

@colinhacks
Copy link
Owner

colinhacks commented Apr 16, 2024

I understand where you're coming from, but I'm afraid that second generic is important. Certain methods like .enum require a properly typed tuple of elements to function properly, so allowing this would a footgun.

Consider using satisfies instead. (It's also worth noting that z.enum doesn't allow numeric literals as in your example so the code below uses z.union().

// Assuming AUnionType was defined somewhere as
type AUnionType = 1 | 2 | 3 | 4;

// If we want to provide explicit type to it, we have to define it like
const myEnum = z.union([
  z.literal(1),
  z.literal(2),
  z.literal(3),
  z.literal(4),
]) satisfies z.ZodType<AUnionType>;

@colinhacks colinhacks closed this Apr 16, 2024
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.

How to make ZodUnion from a enum?
2 participants