-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
queryOptions should to pass initialData with a callback that is potentially returning undefined #7341
Comments
Opened a PR that fixes this: #7351 |
Hi @TkDodo I'm not sure about this one. Code is identical to React adapter and I don't think initialData is supposed to be able to undefined? |
The problematic types are defined in angular-query-experimental. If an undefined return type is not supported than I'm not sure how the common use case will work. This example (from the docs) currently wouldn't work: result = injectQuery(() => ({
queryKey: ['todo', this.todoId()],
queryFn: () => fetch('/todos'),
initialData: () => {
// Use a todo from the 'todos' query as the initial data for this todo query
return this.queryClient
.getQueryData(['todos'])
?.find((d) => d.id === this.todoId())
},
})) Also, this works in react-query: const x = useQuery({
initialData: () => undefined,
}) |
it should work, but it also doesn't work in react query: I don't think your proposed solution is right though. I think we should rather extend export type UndefinedInitialDataOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
- initialData?: undefined
- initialData?: undefined | () => undefined
} thoughts ? |
sorry, that doesn't work. This seems to be the correct diff: - initialData?: undefined
+ initialData?: undefined | InitialDataFunction<TQueryFnData> |
okay interestingly, we have test that cover this case already, and they don't fail: query/packages/react-query/src/__tests__/useQuery.test-d.tsx Lines 79 to 91 in 39b2f81
🤔 |
When looking at the types more closely I believe you are correct. According to my understanding the idea is that I'll fix the PR accordingly. |
It doesn't fail because you use |
I fixed the PR per feedback: #7351 |
Describe the bug
In angular-query
When using
queryOptions
'@tanstack/angular-query-experimental'
and passing an initial value function, typescript will not allow that typescript to return undefined. The following code will not compile:This should be allowed because I might want to check if I have this particular "todo" item in the cache, and if I don't, I would want angular query to request that item. Since this is conditional, I have to put it in a function that might either have a todo or not (in the example I set it to undefined just for the example).
I believe this has to do with this type definition:
Which should be changed to:
Your minimal, reproducible example
https://stackblitz.com/edit/stackblitz-starters-jdj53x?file=src%2Fmain.ts
Steps to reproduce
Expected behavior
The code should compile
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Tanstack Query adapter
angular-query
TanStack Query version
5.32.0
TypeScript version
5.3.0
Additional context
No response
The text was updated successfully, but these errors were encountered: