useMutationState filter predicate variables no longer working with typescript #8395
Send to my agentDescribe the bug#
after doing a "pnpm up" on my project I noticed new typescript errors
"@tanstack/react-query": "^5.59.19" updated to "@tanstack/react-query": "^5.62.2"
It will say "thing does not exist on type {}"
Your minimal, reproducible example#
https://codesandbox.io/p/devbox/relaxed-scooby-9rgqft?workspaceId=ws_VPZaJaqxqdr5erejxx17SR
Steps to reproduce#
Look in the codesandbox on line 43
Expected behavior#
No typescript error, since there were none in the previous react query version
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
kubuntu chromium
Tanstack Query adapter#
react-query
TanStack Query version#
5.62.2
TypeScript version#
5.7.2
Additional context#
No response
TkDodo added labelstypes
The mutation passed to
useMutationStateis of typeMutation<unknown, Error, unknown, unknown>, and I don't think it has ever been anything. The issue title seems to imply that it did work sometime in the past? Can you create a reproduction with a version where this worked?The thing is, this code can match different mutations with different types and different variables:
const importExportMutationState = useMutationState({ filters: { mutationKey: ["importdb"], exact: true, predicate: (mutation) => mutation.state.variables?.fname === "arbitrary filename here", }, })[0];exact: truecan't do any type narrowing, and even if it could,mutationKey: ["importdb"]doesn't tell us that there is a mutation with those variables. So what you want is currently not possible automatically - only by adding manual type annotations touseMutationState.as for
placeholderData- this works fine for me, but you have to make the QueryKey a tuple withas const: https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAOQACMAhgHaoMDGA1gPRTr2swBaAI458VAFDjWEZvBLAANjFyoAXHABK6aVAAmAHhZRgjAOYAaOEZOmA2gF0AfHAC8iQlJks4AdxO6SeigQAEldVzgAIgBGSMk0LFE8AAoEcTgMuBFcPABpdDx1WypWenB6YFNmKksqBWAWGtJFZShUezh6VDhpWXN0zOz8ADFGdWSASldnAAVyEAb0ADoeVAgFADd0ZIBWCf7MuDAFPnQAC3XdXAARegZxsB4N4Ag0W4ZLIbwpl2c0w8OvW8tnM5i++TwwxaKg6bi+AH4luCCnB4fC4A4BgC4JxONiMgA9eGSfFwYAkODJLGkrJJCFQpQqVwuNzIRhXeSMdDhAA+POppJgeDA6AgFORkOhbWZbio1jMVDgfIF+IlDNaqCWfnZgWCYRlKHZ6E53JVGSm-xpGR4MGQUEYho5Jm5AG4zUQSfibXaHWqpZrtQEgqFdEsTKwFMgrqhkoHdSGJu7DujHuhnq9UO96EnMuo2U6ubo3YcPIQJkATkDodo closed this
I see it working in your example, nice :)
Updated the example for
placeholderDatato what matches my issue and get the same result. Can not get it to work unfortunately.We a re using queryOptions approach you shared at some point back in time. Guessing this is where it breaks somehow.
Any more insides? 🤔
For the
useMutationStatehere's an example of how it used to workFor the useMutationState here's an example of how it used to work
with which version of react-query did this work?
We a re using queryOptions approach you shared at some point back in time. Guessing this is where it breaks somehow.
Thanks for showing me this. I think I broke that with:
This is also related to:
I need to find a better way to fix it 😅
The version I updated from was
5.60.5- now using5.62.3.It breaks on
5.61.0The version just before still works.shouldnt this issue be re-opened then?
@bastibuck here’s the playground with types from v5.60.5. As you can see, it also doesn’t work.
To the best of my knowledge, the
mutationin thepredicatefilter was always of typeMutation<unknown, Error, unknown, unknown>.The mutation passed to
useMutationStateis of typeMutation<unknown, Error, unknown, unknown>, and I don't think it has ever been anything. The issue title seems to imply that it did work sometime in the past? Can you create a reproduction with a version where this worked?https://codesandbox.io/p/devbox/relaxed-scooby-9rgqft?workspaceId=ws_VPZaJaqxqdr5erejxx17SR
only change: react query version to an older one that I said in original issue
@bastibuck here’s the playground with types from v5.60.5. As you can see, it also doesn’t work.
To the best of my knowledge, the
mutationin thepredicatefilter was always of typeMutation<unknown, Error, unknown, unknown>.Are you sure this is actually using the types defined in the comment in the playground?
Locally when installing
5.60.5- the
mutationparameter is of type(parameter) mutation: Mutation<any, any, any, unknown> - where the
predicateis defined as(property) MutationFilters.predicate?: ((mutation: Mutation<any, any, any>) => boolean) | undefined
The corresponding type has been changed here https://github.com/TanStack/query/commit/175757afe633c7aa3eb665b95a81944c8c08c754#diff-9701bc1ef4ed4515a08de71beb3a01aac6de69115ceda88ad67995f1d5feab8cR62
- the
right, okay, thanks for digging out the commit. It doesn’t mean it “worked” before. The type was just
any, which is really unsafe, and now it’s “unknown”, which forces you to think about / narrow it a bit. This is a change for the better.what is the proper way to avoid the typescript error then? casting?
(mutation) => (mutation.state.variables as {fname: string})?.fname === fileentry.namemaybe the docs should be updated here then: https://tanstack.com/query/latest/docs/framework/react/guides/filters#mutation-filters
Sign in to join this conversation.
I have the same in a
useMutationState()but also in auseQuery()'splaceholderData(). See below