colinhacks / tozod

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for `Enum` Values

xTVaser opened this issue · comments

Enum values don't seem to be supported currently, a minimal example:

type TestType = {
  enumVar: "val1" | "val2";
};

export const Test: toZod<TestType> = z.object({
  enumVar: z.enum(["val1", "val2"]),
});

Produces the following:

Type 'ZodObject<{ enumVar: ZodEnum<["val1", "val2"]>; }, "passthrough", ZodTypeAny, { enumVar: "val1" | "val2"; }, { enumVar: "val1" | "val2"; }>' is not assignable to type 'ZodObject<{ enumVar: never; }, "passthrough", ZodTypeAny, TestType, TestType>'.
  Type '{ enumVar: z.ZodEnum<["val1", "val2"]>; }' is not assignable to type '{ enumVar: never; }'.
    Types of property 'enumVar' are incompatible.
      Type 'ZodEnum<["val1", "val2"]>' is not assignable to type 'never'.

I suspect this may be related to #4 as under the hood, ZodEnums are just ZodUnions.

Here's a PR with a version that supports enums as well: #27

Do we have an update on this? I am trying to use toZod with a type that has an enum prop. After toZod enum is never.