Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Inferring type with zod.input when using zod.coerce #2172

Closed
nkeysinstil opened this issue Mar 9, 2023 · 4 comments
Closed

Inferring type with zod.input when using zod.coerce #2172

nkeysinstil opened this issue Mar 9, 2023 · 4 comments

Comments

@nkeysinstil
Copy link

nkeysinstil commented Mar 9, 2023

const exampleObject = zod.object({
    date: zod.coerce.date()
});

export type exampleTypeInput = zod.input<typeof exampleObject>;

In this exampleTypeInput inferred for the attribute date has type Date Can this either be 'Any' or an option to add a type? Current issue means that we are unable to see this work with passing in a non Date value for example a String

@JacobWeisenburger
Copy link
Contributor

I think this is being addressed here: #1793

@JacobWeisenburger JacobWeisenburger linked a pull request Mar 9, 2023 that will close this issue
@colinhacks
Copy link
Owner

colinhacks commented Mar 9, 2023

This is unlikely to be fixed in the near future unfortunately #1760 (comment)

You may have to define some helpers like this:

z.any().transform(Number).pipe(z.number())

@JacobWeisenburger
Copy link
Contributor

z.any().transform(Number).pipe(z.number())

If you are going to do the above, then I would recommend doing it this way. Seems cleaner.

const schema = z.any().pipe( z.coerce.number() )
type input = z.input<typeof schema> // any
type output = z.output<typeof schema> // number

@colinhacks
Copy link
Owner

Nice, definitely better

Repository owner locked and limited conversation to collaborators Mar 10, 2023
@JacobWeisenburger JacobWeisenburger converted this issue into discussion #2181 Mar 10, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants