fabien0102 / ts-to-zod

Generate zod schemas from typescript types/interfaces
MIT License
1.24k stars 70 forks source link

SyntaxError: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference #292

Open Christian-Harnisch-DB opened 6 days ago

Christian-Harnisch-DB commented 6 days ago

Bug description

I'm trying to generate Zod schemas from a types.d.ts file. However, no output file is written and the error message "SyntaxError: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference" is logged. This seems to be related to the input file being a d.ts file, but my experience with TypeScript is rather limited so I don't know what the actual issue is.

Input

This file from prometheus-query-js. The (Powershell) CLI command is npx ts-to-zod .\node_modules\prometheus-query\dist\types.d.ts .\build\types.zod.ts. Adding --skipValidation to the CLI command just suppresses the line "√ Validating generated types".

Expected output

A file with Zod schemas

Actual output

√ Validating generated types
    SyntaxError: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference. (5:43)
      3 | import { ResponseType } from "./../node_modules/prometheus-query/dist/types.d";
      4 |
    > 5 | export declare const responseTypeSchema = z.nativeEnum(ResponseType);
        |                                           ^
      6 |
      7 | export declare const serieSelectorSchema = z.union([z.string(), z.array(z.string())]);
      8 |

Versions

tvillaren commented 4 days ago

Hello,

I've never used the lib with a d.ts type declaration file but: