Replies: 2 comments 6 replies
-
I use the following approach, and it's been working well so far: // schema.ts
import { z } from "zod";
export const USER = z.object({ name: z.string(), type: z.enum(["Person", "Robot"]) });
export interface User extends z.infer<typeof USER> {}
// or export type User = z.infer<typeof USER>; // whatever.ts
import { User } from "./schema.ts";
export const (userType: User["type"]) => {
...
};
|
Beta Was this translation helpful? Give feedback.
1 reply
-
Have you considered using nativeEnum? https://zod.dev/?id=native-enums This way you can define the enum in 1 place and use it throughout your app without having to go through the User |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey guys!
Just discovering zod - so let's say I have a schema like
and then I have a function somewhere like
What do I replace
XXX
with?I don't want to have to define an external
enum
since I already have it defined with Zod from before. Also I'm not going to writez.infer<typeof userSchema.shape.type>()
everywhere since it's ugly?Just trying to wrap my head around that typing problem, since
z.object
andz.enum
don't return types but Zod objects so they are incompatible with the rest of my app for typing purposes and I don't wanna have duplicated stuff.So I guess I'm missing something here.
Thanks in advance for your help!
Beta Was this translation helpful? Give feedback.
All reactions