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

Have types exports on generated file? #81

Closed
SrBrahma opened this issue May 30, 2022 · 7 comments
Closed

Have types exports on generated file? #81

SrBrahma opened this issue May 30, 2022 · 7 comments

Comments

@SrBrahma
Copy link

SrBrahma commented May 30, 2022

Feature description

The generated file only contains the schemas. I would have no problemas for my small files to have the export type MyType = z.infer<typeof myTypeSchema>. Looks like I have to add them manually to the generated file.

Input

export type MyType = z.number();

Output

export const myTypeSchema = z.number();
export type MyType = z.infer<typeof myTypeSchema>;
@fabien0102
Copy link
Owner

Hello!

Just for challenge a bit the feature request, what is your usecase? Normally you should already have the original types around (since this is the source for ts-to-zod) and those types will always be better since they have the documentation for example.

image

You will loose this usefull piece of information if you retrieve the type with z.infer

@SrBrahma
Copy link
Author

Hi!

Typescript is smart!

image

Even using infer, it keeps the JSDoc comments from the schema!

@SrBrahma
Copy link
Author

Also, I wouldn't trust having a separated schema from the type.

If I change the type but forget to change the schema, there would be bugs.

@fabien0102
Copy link
Owner

Good to know that typescript can follow! Quite amazing indeed!

Regarding the trust issue, my solution is usually to check this on CI, as example in this repository:
image

So if something is not in sync, typescript will throw (you can also check if the git diff is empty)

Is this could solve your issue?

@SrBrahma
Copy link
Author

But, if you add extra stuff to the schemas (as the new .trim() to strings) they would be overwritten, right?

@fabien0102
Copy link
Owner

fabien0102 commented Jun 1, 2022

Ah ok, now I get your usecase! you are mutating the generated zod schema! If this is the case, this was not really the intented usage (I'm always happy to be challenged). The idea was to keep the generated file as source of truth and sync with the typescript definition and import/extends the schemas in a separate file. But regarding your screenshot, I get why this is probably not possible in your case.

Looks like I have to add them manually to the generated file.

But now everything make sense 😅

Anyway, this should not be too hard adding an option to generate those infer, we just need to make sure to not have any name conflicts when the types are recursive (https://github.com/colinhacks/zod#recursive-types)

@Ionshard
Copy link

Ionshard commented Oct 27, 2023

I came across this tool while looking for something to help us migrate towards Zod with some of our types. We don't really want to keep them in sync, we're looking to move toward Zod as the single source of truth. I understand that this was not the original usecase but this would be really helpful for people who want to do one-time migrations.

EDIT: Oh I see there is a linked PR that actually adds this feature. I will look into that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants