Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bug: TRPC's inferred mutation input type is incorrect when used with zod4's preprocessΒ #7035

@kirbyfu

Description

@kirbyfu

Provide environment information

trpc 11.7.2
zod 4.1.13

Describe the bug

In the example repository, we define a schema that uses z.preprocess:

const createEntitySchema = z.object({
  name: z.string(),
  description: z.preprocess(
    (str) => (str === '' ? null : str),
    z.string().nullish()
  ),
});

`type CreateEntity = z.infer<typeof createEntitySchema>;` 

The inferred type computes to:

type CreateEntity = {
    name: string;
    description?: string | null | undefined;
}

However when we access the mutation via TRPC's proxy wizardry, we get a type error since it thinks that description is required and of type unknown

  const { mutate } = trpc.createEntity.useMutation();

  const onNothing = () => {
    mutate({
      name: 'hello',
    });
  };
Image

I believe this isn't an issue when using zod v3

Link to reproduction

https://stackblitz.com/edit/github-lxakaelx?file=src%2Fpages%2Fapi%2Ftrpc%2F%5Btrpc%5D.ts

To reproduce

https://stackblitz.com/edit/github-lxakaelx?file=src%2Fpages%2Fapi%2Ftrpc%2F%5Btrpc%5D.ts

Running next build will show the type error

Additional information

No response

πŸ‘¨β€πŸ‘§β€πŸ‘¦ Contributing

  • πŸ™‹β€β™‚οΈ Yes, I'd be down to file a PR fixing this bug!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions