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

z.infer type vs z.input #91

Open
amritk opened this issue Nov 14, 2024 · 2 comments · May be fixed by #92
Open

z.infer type vs z.input #91

amritk opened this issue Nov 14, 2024 · 2 comments · May be fixed by #92

Comments

@amritk
Copy link

amritk commented Nov 14, 2024

Hello! I was using using it like this

import { z } from 'zod'
import { createTypeAlias, printNode, zodToTs } from 'zod-to-ts'

const schema = z.object({
  required: z.string(),
  optional: z.string().optional(),
  default: z.string().optional().default('default'),
})

const { node } = zodToTs(schema, 'Test')
const typeAlias = createTypeAlias(node, 'Test')
console.log(printNode(typeAlias))

type Infer = z.infer<typeof schema>
type Input = z.input<typeof schema>

It prints out default with a ?

type Test = {
    required: string;
    optional?: string | undefined;
    default?: string;
};

type Infer

type Infer = {
    required: string;
    default: string;
    optional?: string | undefined;
}

type Input

type Input = {
    required: string;
    optional?: string | undefined;
    default?: string | undefined;
}

In infer, the type of default is not optional. Is there an option or something to get the type of z.infer?

@amritk
Copy link
Author

amritk commented Nov 14, 2024

It looks like it should be doing this actually

https://github.com/sachinraja/zod-to-ts/blob/main/src/index.ts#L363

Let me see if I can find the issue

@amritk amritk linked a pull request Nov 14, 2024 that will close this issue
@mathcovax
Copy link

Hello @amritk, feel free to check out this library zod-to-typescript. it should address your issue thanks to its Hook system.

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

Successfully merging a pull request may close this issue.

2 participants