Skip to content
Source
Sign inSign up
TanStack/query

Typescript 5.4.5 and useInfiniteQuery requires avoidable type annotations #7495

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

Describe the bug

When using the latest Typescript and latest React Query, useInfiniteQuery requires convoluted type annotations to avoid errors. These should not be necessary and should be inferred by the compiler. Specifically, the configuration option initialPageParam should infer it's type by assignment, but it does not and infers unknown unless verbose typing is declared at the useInfiniteQuery call. This behavior is illustrated in the linked Playground example. Note the:

useInfiniteQuery<Post[]>({

vs

useInfiniteQuery<Post[], Error, InfiniteData<Post[]>, QueryKey, number>({

The first is used in many online tutorials with apparently no error marks, but I can only get rid of the errors by using the second typed invocation.

Your minimal, reproducible example

https://www.typescriptlang.org/play/?ts=5.4.5#code/JYWwDg9gTgLgBAbwFBzgawKYbABShgN2AgFcBnAEQEMYqAaFOcjASQDsAzYN4GDARRIYoATwap2XHn2q1xcQcJEBpDGKQBfOBygQQcAOQABWmzK0AxmgD0+KhZgBaAI5DRBpKEiw4VAB7EZNq6+gb+gR5IGH7e8Nx8UBz2GHA4EOaIjMAAJgBccGwkIABGwowwvAA2GPnmUNwA5ozFENkitTD1bE2ozFAseQVFpVCaSJ5sCUkWKWnmiqKZqGBUDRgAysAAXjVDJWUa40gWEGbwzHMwQQC8cAAUrkr5lwsiAJRw1wB8jMyS3LwBG4RAAeS4AbQAunQ4ABRKC6KBfO7IVCoR6iVTtODggyQcxkAwwjEiaGMdHAgBibHyKLgKzWOCoUCo+luAEY4BoPt8lmjUPgYCQoGxfAF0uT+XAAHRrGBg9IwKHIgwACxgMDAZFy1msACsyKcwJVkqqIJVssJpTARGBgCdLdKTiBrPirkS+VK0SsWSBtZ6vWiAPrmZkwWkMjBM31wRxwdkfABUcBJ0sjmx28kDwcqoF4+VT6e2GCzgY0pf53Ml-OtqowbDud3wZB5Xzgzel2RoVDekvLktD1QAKqBduyAAxB8fT0sAipUSpMxnM1n5dml42m82WqCyKj5TDYPCEYjkPeluUAOWiMCXUZXIFpJvMd5hC8XqwwZBhz9vn+jrKtgGAoYEKIpwL+d7StU3QwKqcBtuOcAAPy+JUH5rGQ0H1g0cFwAA1PGcD5CQbCWlIGDZH2F6gceRCkGQd4AY+9xcFAL6fm+6F3t+EFUBxy6+q21ZoRhX7YbB8FtpyqHvjxEm4fBcaciRZEYBR2TyFW4wnGcTBkFGipkAATJ89wks8iqvMJvQGf80hAkoCrmFCMLwoiML2YCe7OUqkJfDCrxYjChT7EiKKSiSWL5LibqEsSwJkvyJLUrSCD0v+D5mZy3KfG2qJSoKwqiuEErZrKoG+cqdxqhqWo6vqhpsJuMxmhaVo2naDoYE6eiukZHoFdmPqsv6Q3ZnAIa0LAEaZTGylJimwJpp+GYliJXpBrmID5ktSgrWsa0VlK-bZlW5VwfWjbNkBHZdrQvaVqWg4YCOIBjpO07jrO0jAAuTEPmuG4mq127CHuB5YLg+D0We3Y0TA15+H+gmrvckGcaJPE-vxKP3kJeXAe2oHFXxAk9TBikIXASGydxn5YZTeGESpTBqRp1GSnKdGnoxc1o3cbHk1xYm8RjqMgDZUpyQzCl4dJKFY7LTNKURqnkdwlFab2UQxNA8AIBcRkwkbBLGRoQA

Steps to reproduce

  1. Navigate to TS Playground link provided
  2. Note the error marks and lack-there-of in the 2 hook implementations provided

Expected behavior

As a developer, I expect the only required type annotation to be useInfiniteQuery<**TQueryFnData**> the others should be forwarded or inferred by the value of initialPageParam

How often does this bug happen?

Every time

Screenshots or Videos

image

Platform

macOS Sonoma 14.5, VS Code Version: 1.89.1 (Universal) @tanstack/react-query@5.40.0 @types/react-dom@18.3.0 @types/react@18.3.3 @vitejs/plugin-react@4.3.0 axios@1.7.2 react-dom@18.3.1 react@18.3.1 typescript@5.4.5 vite@5.2.12

Tanstack Query adapter

react-query

TanStack Query version

v5.40.0

TypeScript version

v5.4.5

Additional context

No response

  1. TkDodo closed this

  2. TkDodo added labelsinvalid

  3. TkDodo added labelsworks as designed

  4. TkDodo added labelstypes

  5. Wow would have never guessed this, but the order of the properties was also my issue as well. Thanks :)

Sign in to join this conversation.