Skip to content

Generated defaults for enums type results in typescript errors - wrong type #331

@VilleViktor

Description

@VilleViktor

Bug description

When generating schemas with enum that has default values, typescript errors occurs:

No overload matches this call.
  Overload 1 of 2, '(def: MyEnum): ZodDefault<ZodEnum<typeof MyEnum>>', gave the following error.
    Argument of type '"THREE"' is not assignable to parameter of type 'MyEnum'.
  Overload 2 of 2, '(def: () => MyEnum): ZodDefault<ZodEnum<typeof MyEnum>>', gave the following error.
    Argument of type 'string' is not assignable to parameter of type '() => MyEnum'.ts(2769)

8   myEnum: myEnumSchema.default('THREE')
                                 ~~~~~~~

Input

enum MyEnum {
    One = 'ONE',
    Three = 'Three'
 }

interface MyType {
  /**
   * @default THREE
   */
   myEnum: MyEnum
 }

Expected output

// Generated by ts-to-zod
import { z } from 'zod'
import { MyEnum } from './test'

export const myEnumSchema = z.enum(MyEnum)

export const myTypeSchema = z.object({
  myEnum: myEnumSchema.default(MyEnum.Three)
})

Actual output

// Generated by ts-to-zod
import { z } from 'zod'
import { MyEnum } from './test'

export const myEnumSchema = z.enum(MyEnum)

export const myTypeSchema = z.object({
  myEnum: myEnumSchema.default('THREE')
})

Versions

  • "typescript": "~5.9.2",
  • "zod": "^4.1.5"
  • "ts-to-zod": "^4.0.0",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions