Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem getting imports to work #242

Closed
pkyeck opened this issue May 22, 2024 · 4 comments · Fixed by #243
Closed

Problem getting imports to work #242

pkyeck opened this issue May 22, 2024 · 4 comments · Fixed by #243
Labels
bug Something isn't working

Comments

@pkyeck
Copy link

pkyeck commented May 22, 2024

Bug description

I'm trying to generate zods from our types. We have a lot of files and some of them use types from others. I wrote a script collecting all the filenames and generating a config file (like mentioned here #151) ... looks good so far ... but. I have a problem with the import of another type from a different file:

// a.types.ts
export enum TemplateType {
  Accordion = "accordion",
  AlphabetFilter = "alphabet_filter",
  ArticleHeader = "article_header",
  Audio = "audio",
}
// b.types.ts
import { TemplateType } from "./a.types";

export type TAudioData = {
  a: string;
  b?: string;
};

export type TAudioRequest = {
  _template: TemplateType.Audio;
} & TAudioData;

and my config looks like this:

module.exports = [
  { name: "a", input: "src/a.types.ts", output: "src/a.zod.ts" },
  { name: "b", input: "src/b.types.ts", output: "src/b.zod.ts" },
];

If I now run yarn ts-to-zod --all I get the following error:

$ ts-to-zod --all
Generating "a"
Generating "b"
 ⚠ File "src/a.zod.ts" not found: maybe it hasn't been generated yet?
 ⚠ File "src/b.zod.ts" not found: maybe it hasn't been generated yet?
✖ Validating generated types
 ›   Error: 'tAudioRequestSchema' is not compatible with 'TAudioRequest':
 ›   Argument of type '{ [x: string]: any; _template?: unknown; } & { a: string; b?: 
 ›   string | undefined; }' is not assignable to parameter of type 'TAudioRequest'.
 ›     Type '{ [x: string]: any; _template?: unknown; } & { a: string; b?: string | 
 ›   undefined; }' is not assignable to type '{ _template: TemplateType.Audio; }'.
 ›       Types of property '_template' are incompatible.
 ›         Type 'unknown' is not assignable to type 'TemplateType.Audio'.

✔ Validating generated types
 🎉 Zod schemas generated!

✨  Done in 1.27s.

If I put all types in one file and run ts-to-zod no error is thrown – so I guess I broke the imports 🤷‍♂️
This should work, right?! Is my config not correct or why am I getting these weird errors?

Versions

"ts-to-zod": "^3.8.5"

@pkyeck
Copy link
Author

pkyeck commented May 22, 2024

Ah, forgot to mention, that I uploaded these to a repo as well: https://github.com/pkyeck/test-ts-to-zod

@tvillaren tvillaren added the bug Something isn't working label May 22, 2024
@tvillaren
Copy link
Collaborator

Hello @pkyeck

Indeed, the validation fails because the generated file (which you can check by passing the --skipValidation flag to the command call) looks like this:

// Generated by ts-to-zod
import { z } from "zod";

import { templateTypeSchema } from "./a.zod";

export const tAudioDataSchema = z.object({
  a: z.string(),
  b: z.string().optional(),
});

export const tAudioRequestSchema = z
  .object({
    _template: z.literal(TemplateType.Audio),
  })
  .and(tAudioDataSchema);

The imported import { templateTypeSchema } from "./a.zod"; should be import { TemplateType } from "./a.types";

So it looks like there is an issue with enum imports here...

@pkyeck
Copy link
Author

pkyeck commented May 23, 2024

Ah, thank you. The --skipValidation is really helpful.

"Problem" with enums is, that you can use them as types or values, so we could need both the schema but also the enum itself...

So it looks like there is an issue with enum imports here...
But am I doing something wrong with the export/import enums or is this a bug with the lib?

@tvillaren
Copy link
Collaborator

But am I doing something wrong with the export/import enums or is this a bug with the lib?

I think it's a bug (or a missing feature 😅)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants