DefaultError type fallback type has too strong guarantees #7872
Send to my agentDescribe the bug#
The type of error is by default Error. When using persistQueryClient it will serialize to JSON. Error is not serializable. JSON.stringify(new Error('some message')) will return "{}".
This leads persisted errors to be of type {}, but useQuery defaults to Error. Therefore the type fallback is incorrect.
Your minimal, reproducible example#
Steps to reproduce#
- use
useQuery - look at the type of
error
Expected behavior#
The type of error defaults to Error, expected unknown.
How often does this bug happen?#
Sometimes
Screenshots or Videos#
No response
Platform#
- OS: Android, iOS
- Framework: React Native
Tanstack Query adapter#
react-query
TanStack Query version#
5.51.16
TypeScript version#
5.0.4
Additional context#
I actually notice the types are as expect in my reproducable code, but looking at the code of the latest version of this package the fallback is still Error.
sorry, we're not gonna revert back to
unknown. TheErrortype assumption is "optimistic", yes, because it holds true for 99% of cases. It also falls apart if youtrow 25in yourqueryFn, because25is also not anError.We've documented how you can opt out of that typings here:
https://tanstack.com/query/v5/docs/framework/react/typescript#registering-a-global-error
Basically, you want:
import '@tanstack/react-query' declare module '@tanstack/react-query' { interface Register { defaultError: unknown } }Regarding persistence: We don't persist queries that are in
"error"state for that exact reasons: errors aren't persistable. So to get to anErrorbeing persisted with the plugin, you have to opt-in by implementing theshouldDehydrateQueryoption. This is also documented here: https://tanstack.com/query/v5/docs/framework/react/reference/hydration#limitationsTkDodo closed this
TkDodo added labelswontfix
@TkDodo, allright, clear alternate solution. Thanks for the rapid response, no need to be sorry.
Sign in to join this conversation.
I've submitted a PR with a potential solution: https://github.com/TanStack/query/pull/7873