Skip to content
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

[react-query] type error for infinity query defaults in QueryClient #8218

Open
aizerin opened this issue Oct 25, 2024 · 5 comments
Open

[react-query] type error for infinity query defaults in QueryClient #8218

aizerin opened this issue Oct 25, 2024 · 5 comments

Comments

@aizerin
Copy link

aizerin commented Oct 25, 2024

Describe the bug

I recently migrated from react-query v4 and this code now reports typescript error.

It does not accept values for infinite query.

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      getNextPageParam: (lastPage: any) => lastPage?.nextCursor ?? undefined,
    },
  },
});
Type '{ getNextPageParam: (lastPage: any) => any; }' is not assignable to type 'OmitKeyof<QueryObserverOptions<unknown, Error, unknown, unknown, QueryKey, never>, "suspense" | "queryKey", "strictly">'.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/jtqm8f

Steps to reproduce

Just write this code

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      getNextPageParam: (lastPage: any) => lastPage?.nextCursor ?? undefined,
    },
  },
});

Expected behavior

Typescript should be happy

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Any

Tanstack Query adapter

react-query

TanStack Query version

5.59.16

TypeScript version

5.6.3

Additional context

No response

@aizerin
Copy link
Author

aizerin commented Oct 25, 2024

I know, and I think this is wrong. Defaults for infinity query should be included in types (as they were in react-query v4).

It also works at runtime. getNextPageParam is used as the default for useInfinityQuery.

@pawel-twardziak
Copy link
Contributor

Ok, then it is kinda inconsistency because the implementation differs from its typings.

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 3, 2024

we moved infinite query types into their own interface because getNextPageParam is now required for those:

export interface InfiniteQueryObserverOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> extends QueryObserverOptions<
TQueryFnData,
TError,
TData,
InfiniteData<TQueryData, TPageParam>,
TQueryKey,
TPageParam
>,
InfiniteQueryPageParamsOptions<TQueryFnData, TPageParam> {}

It therefore doesn’t really make sense to set it globally (you’ll get an error on useInfiniteQuery if you don’t provide it) - same as for initialPageParam.

We could add it as Partial to the defaultOptions but how do you intend to use it on useInfiniteQuery then ?

@aizerin
Copy link
Author

aizerin commented Nov 9, 2024

I have several infinity queries with the same getNextPageParam function. So I don't want to repeat it each time and just set it as default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants