Skip to content
Source
Sign inSign up
TanStack/query

Order matters to useInfiniteQuery options and TS #7268

Send to my agent
Closedalburdette619(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe the bug

I am implementing the sort-keys eslint rule in my codebase and it seems that the order of the options given to useInfiniteQuery matter. If I don't have getNextPageParam and getPreviousPageParam after queryFn I get a TS error on queryFn stating

No overload matches this call.

Fine:

useInfiniteQuery({
  enabled: true,
  initialPageParam: "",
  queryFn: async () => {
    const data = await getThings();
    return { links, things: data };
  },
  queryKey: "key",
  getNextPageParam: ({ links }) => links?.next,
  getPreviousPageParam: ({ links }) => links?.previous,
})

Errors

useInfiniteQuery({
  enabled: true,
  getNextPageParam: ({ links }) => links?.next,
  getPreviousPageParam: ({ links }) => links?.previous,
  initialPageParam: "",
  queryFn: async () => {
    const data = await getThings();
    return { links, things: data };
  },,
  queryKey: "key",
})

I tried giving types to useInfiniteQuery but this didn't help.

Your minimal, reproducible example

https://snack.expo.dev/5TZgw1IVp4_3SJXK7PF5R

Steps to reproduce

I've attached the code as an expo snack, but without TS this won't cause any issues and is functional.

  1. Clone the snack or start a new TS project and copy in the code.
  2. Ensure the ordering of the useInfiniteQuery options is alphabetical.
  3. Observe the TS error.
  4. Move the getNext/PreviousPageParam keys to the bottom of the options object.
  5. Observe the TS error goes away.

Expected behavior

Order of options in an object should not matter for automatic typing and TS compatibility.

How often does this bug happen?

Every time

Screenshots or Videos

Screenshot 2024-04-11 at 1 40 09 PM

Platform

  • OS: macOS
  • Browser: React Native

Tanstack Query adapter

react-query

TanStack Query version

v5.24.1

TypeScript version

v5.3.3

Additional context

I tried updating to latest react-query & typescript but didn't see a difference, though it may be more complex of a dependency chain issue if that's it.

  1. TkDodo closed this

  2. TkDodo added labelsduplicate

  3. @TkDodo there is no explanation in that closed duplicate as to why this is happening and no plans to fix.

    Can you give me at least some explanation? The other thread looks like you're talking to people with a ton of context and just say 🤷🏻‍♂️

  4. it's a TypeScript limitation. Have a look at the minimal reproduction with consume / produce posted by Mateusz here:

    produce === queryFn - it produces the return type for TData consume === getNextPage - it consumes the type from queryFn as input data

    in some situations, consume can't come before produce in an object. Again, the only way to fix this is to wait until typescript has fixed this limitation, in which case it will just start to work. There is nothing we can do here.

  5. @TkDodo thanks, I appreciate the response 🙂

Sign in to join this conversation.