Skip to content

Validation error with union of backtick string constants (or just string constants) #340

@zacaj

Description

@zacaj

Bug description

when using a field in my type with a string constant that uses backticks instead of quotes, the generated type validation fails. Depending on if it's a single literal or a union, there's two different errors (shown in one code block below but really done separately)

(this is mostly a problem because our formatter is set up to prefer `` in all cases over ' and ")

Input

{
  union: `move` | `copy`;

  constant: `move`;
}

Expected output

  union: z.union([z.literal("move"), z.literal("copy")]),

  constant: z.literal("move"),

Actual output

✔ Validating generated types
SyntaxError: Unexpected token (16:28)
> 16 | importMode: z.union([z.move(), z.copy()]).default("copy"),

and

✖ Validating generated types
› Error: 'configSchema' is not compatible with 'Config':
› Argument of type '{ [x: string]: any; ... 4 more ...; }' is not assignable to parameter of type 'Config'.
› Property 'constant' is missing in type '{.... }' but required in type 'Config'.

Versions

"ts-to-zod": "^4.0.1",
"typescript": "^5.9.2"

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