zod-to-ts icon indicating copy to clipboard operation
zod-to-ts copied to clipboard

Unable to get the correct type when I used `z.transform()`

Open zce opened this issue 1 year ago • 1 comments

When I use transform to convert the data type, I am not able to obtain the correct type.

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

// define your Zod schema
const UserSchema = z.object({
  username: z.string(),
  age: z.number(),
  avatar: z.string().transform((value) => {
    // convert...
    return {
      src: value,
      width: 100,
      height: 100,
    };
  }),
});

// pass schema and name of type/identifier
const { node } = zodToTs(UserSchema, 'User');

const code = printNode(node);

console.log(code);
{
    username: string;
    age: number;
    avatar: string;
}

zce avatar Nov 17 '23 11:11 zce

I can obtain the correct type inference using z. infer<typeof xxx>.

image

I'm very sorry, I haven't had time to carefully read your project yet. I discovered this issue on my first use

zce avatar Nov 17 '23 11:11 zce