T
TanStack9mo ago
rare-sapphire

Broken server function type safety?

I have a server function called:
const signInWithGoogle = createServerFn({ method: "POST" })
.validator((data: unknown) =>
z.object({ code: z.string(), state: z.string() }).parse(data)
)
.handler(async ({ data }) => ...);
const signInWithGoogle = createServerFn({ method: "POST" })
.validator((data: unknown) =>
z.object({ code: z.string(), state: z.string() }).parse(data)
)
.handler(async ({ data }) => ...);
data is typed correctly within the handler but when I call it via:
const { mutateAsync } = useMutation({
mutationFn: signInWithGoogle,
});
await signInWithGoogle();
const { mutateAsync } = useMutation({
mutationFn: signInWithGoogle,
});
await signInWithGoogle();
no errors are displayed even though the object is required. What am I doing wrong? Thanks
5 Replies
sunny-green
sunny-green9mo ago
data is unknown as the input type it would need to be typed differently to get a type error here to avoid this, just supply the zod schema as validator without wrapping it
rare-sapphire
rare-sapphireOP9mo ago
If you mean .validator(z.object({ code: z.string(), state: z.string() })) then that yields the same result Why is that? And if so, why is it typed correctly within the handler?
sunny-green
sunny-green9mo ago
are you using latest zod?
sunny-green
sunny-green9mo ago
Server Functions | TanStack Router React Docs
What are Server Functions? Server functions allow you to specify logic that can be invoked almost anywhere (even the client), but run only on the server. In fact, they are not so different from an API...
rare-sapphire
rare-sapphireOP9mo ago
Upgrading from 3.23 to 3.24.1 solved the issue. Thanks 👍

Did you find this page helpful?