-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[vue-query] Type error returning undefined from initialData
in queryOptions
#9069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Currently the implementation is: export type UndefinedInitialQueryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey> & {
initialData?: undefined
} so in your example if you set |
@saul-atomrigs Yeah but Same exact code in react-query doesn't produce any errors. |
I think the equivalent type in react-query is the following, which has a few more things: export type UndefinedInitialDataOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData?:
| undefined
| InitialDataFunction<NonUndefinedGuard<TQueryFnData>> // *** this
| NonUndefinedGuard<TQueryFnData> // *** and this
} Should vue-query add these lines? |
yeah probably. |
Thank you @saul-atomrigs for the quick fix. Closing as completed by #9073. |
Will have to reopen. Just tried upgrading in our codebase and I'm afraid #9073 caused type regression for the case when
I'm worried this may be Vue's limitation. |
@TkDodo @DamianOsipiuk export type UndefinedInitialQueryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey> & {
initialData?: undefined
| (() => undefined) // add this
// | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
// | NonUndefinedGuard<TQueryFnData>
} Let me know if there's a better approach |
I'm sorry but the original issue is now back. Can we please reopen @DamianOsipiuk? Using the following type test case from I think I know what's going on and how to fix that properly for both |
…ver type safety of initialData in "queryOptions-to-useQuery" interaction
… useQuery to come before general ones (TanStack#9069) This instructs TypeScript to pick correct function overload when initialData is defined.
… useQuery to come before general ones (TanStack#9069) This instructs TypeScript to pick correct function overload when initialData is defined.
Describe the bug
In vue-query if
initialData
function returns undefined typescript error is displayed.According to the docs this should be allowed https://tanstack.com/query/latest/docs/framework/react/guides/initial-query-data#conditional-initial-data-from-cache and query should fetch from hard loading state instead.
Your minimal, reproducible example
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgRwK4FMoE8DyYbAQB2AznAL5wBmUEIcARAAIwCGpbAxgNYD0AbhgC0aTFgYBuAFBTgRGJiqtO6OAGFiVYAHNEUuHH6sANhgBccEjChzt08jKqoinAsTjb0MDUS3aAFACUegZQXqhQRHAACrQgwCToAHRhJBDG-Oj+SEam6BYA5AAW6MbGEAUUcKxkPn6B9jIAJuicxqxhHuUARiYhcHIKUEoqcACSRMAEJgAirGz9BpyaOgD8FnU60gYO+oYdcAD6h3JTwLPzrBYTZxdsjVLLHHAQ+ISkcAC8KBjYeG6kbJ7UTYADS6CwFgA2gwnn4GABdapkJ5WAA0wN+WAAYkQLJ5vCttBiDKdpsY5mwLEEvgA+RZwMIwCJRbQ9EwAFSKCSSxzJ5wplyScK2e3IGPIDSAA
Steps to reproduce
initialData
Expected behavior
initialData
allows to return undefined. No type errors produced.How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
N/A
Tanstack Query adapter
vue-query
TanStack Query version
v5.74.6
TypeScript version
v5.8.3
Additional context
Query still fetches just fine from hard loading state so the logic is correct. Also no type errors in
react-query
for the exact same code.The text was updated successfully, but these errors were encountered: