TanStack / query

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
https://tanstack.com/query
MIT License
42.67k stars 2.92k forks source link

[react-query] Type Inference Bug When Defining getNextPageParam Before queryFn in useInfiniteQuery #8301

Closed jijiseong closed 1 week ago

jijiseong commented 1 week ago

Describe the bug

When Defining getNextPageParam Before queryFn in useInfiniteQuery (same issue with infiniteQueryOptions) lastPage is inffered as unknown

It works well

  useInfiniteQuery({
    queryKey: ["data"],
    initialPageParam: 1,
    queryFn: ({ pageParam }) => getData(pageParam),
    getNextPageParam: (lastPage) =>
      lastPage.hasNext ? lastPage.page + 1 : undefined,
  });

It doesn’t work well. lastPage in getNextPageParam is inffered as unknown

  useInfiniteQuery({
    queryKey: ["data"],
    initialPageParam: 1,
    getNextPageParam: (lastPage) =>
      lastPage.hasNext ? lastPage.page + 1 : undefined,
    queryFn: ({ pageParam }) => getData(pageParam),
  });

Your minimal, reproducible example

https://codesandbox.io/p/devbox/dkhrfg

Steps to reproduce

  1. Go to codesandbox link

Expected behavior

I expect that lastPage is inferred as the return type of getData.

  useInfiniteQuery({
    queryKey: ["data"],
    initialPageParam: 1,
    getNextPageParam: (lastPage) =>
      lastPage.hasNext ? lastPage.page + 1 : undefined,
    queryFn: ({ pageParam }) => getData(pageParam),
  });

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

not affect

Tanstack Query adapter

None

TanStack Query version

latest

TypeScript version

v5.6.3

Additional context

No response

TkDodo commented 1 week ago

This is a known issue because of how TypeScript currently infers types. We have a lint rule that should catch this:

https://tanstack.com/query/v5/docs/eslint/infinite-query-property-order