pingdotgg / uploadthing

File uploads for modern web devs
https://uploadthing.com
MIT License
4.26k stars 315 forks source link

[bug]: zod transforms not typed properly #1040

Closed PatrikTrefil closed 2 weeks ago

PatrikTrefil commented 2 weeks ago

Provide environment information

System:
    OS: Windows 11 10.0.26100
    CPU: (8) x64 Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
    Memory: 2.55 GB / 15.59 GB
  Binaries:
    Node: 20.17.0 - C:\Program Files\nodejs\node.EXE      
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (130.0.2849.56)
    Internet Explorer: 11.0.26100.1882
  npmPackages:
    @uploadthing/react: ^7.0.3 => 7.1.0
    typescript: ^5.5.3 => 5.6.3
    uploadthing: ^7.1.0 => 7.2.0

Describe the bug

When transforming input using zod, the input types is inferred incorrectly. As an example, let's say our input is a string, that will be transformed to a number using zod. When we try to use this in client code, we get a type error.

// router.ts
const buildRoute = createUploadthing();

export const fileRouter = {
    myUploader: buildRoute({...})
        .input(z.object({ a: z.string().transform((arg) => Number(arg)) })))
        .onUploadComplete(...)
};
// client.ts
const {startUpload} = useUploadThing("myUploader");
...
startUpload(files, { a: "1" }); // Gives a type error, because it wants a to be of type Number

I guess the problem is that the underlying code is using z.infer<> instead of z.input<>.

Link to reproduction

https://stackblitz.com/edit/github-ikbntz?file=src%2Fserver%2Fuploadthing.ts

To reproduce

The stackblitz is not fully functional, because I could not figure out how to add zod as a dependency. I am sure it's clear what the issue is though. If you add zod as a dependency, just look at the type error when providing the input to the startUpload function.

Additional information

No response

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

Code of Conduct