I have this object:
const properties = [
{ value: "entire_place", label: "The entire place" },
{ value: "private_room", label: "A private room" },
{ value: "shared_room", label: "A shared room" },
] as const;
I need to use it with zod in order to
- Validate and parse my data on the backend
- Create a typescript union type with those possible values
"entire_place" | "shared_room" | "private_room"
According to the zod documentation, i can do this:
const properties = [
{ value: "entire_place", label: "The entire place" },
{ value: "private_room", label: "A private room" },
{ value: "shared_room", label: "A shared room" },
] as const;
const VALUES = ["entire_place", "private_room", "shared_room"] as const;
const Property = z.enum(VALUES);
type Property = z.infer<typeof Property>;
However, I don't want to define my data twice, one time with a label (the label is used for ui purposes), and another without a label.
I want to define it only once using the properties
object, without the VALUES
array, and use it to create a zod object and infer the type from the zod object.
Any solutions how?