-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Describe the bug
Typescript infers type of context as unknown if onMutate
comes after onSuccess
, onError
or onSettled
, in the following codeblock mutation1
typescript reports the type of context to be unknown
which is wrong, in mutation2
typescript reports type of context to be { foo: string } | undefined
which is expected and correct
const mutation1 = createMutation(() => ({
onError: (error, variables, context) => console.log(context.foo),
onMutate: ({}) => ({ foo: 'bar' }),
onSuccess: (data, variables, context) => console.log(context.foo),
}));
const mutation2 = createMutation(() => ({
onMutate: ({}) => ({ foo: 'bar' }),
onError: (error, variables, context) => console.log(context.foo),
onSuccess: (data, variables, context) => console.log(context.foo),
}));
Your minimal, reproducible example
https://stackblitz.com/edit/github-iy58hbhh?file=src%2Froutes%2Findex.tsx
Steps to reproduce
- Go to the stackblitz url
- Make sure node_modules is installed
- Goto src/routes/index.tsx and hover over the context errors
Expected behavior
The order of keys don't matter and type of context is correctly inferred, I understand the workaround is pretty simple but I use a prettier plugin that orders all object keys alphabetically which is good for stable git diffs
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
OS: Linux vc-arch 6.12.10-zen1-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Sat, 18 Jan 2025 02:26:52 +0000 x86_64 GNU/Linux
Browser: N/A
Editor: Neovim
Tanstack Query adapter
solid-query
TanStack Query version
5.66.0
TypeScript version
5.7.3
Additional context
It only happens if the argument list of onMutate
is non empty