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

Typescript 5.4.5 and useInfiniteQuery requires avoidable type annotations #7495

Closed
izkrivera opened this issue May 30, 2024 · 3 comments
Closed
Labels

Comments

@izkrivera
Copy link

izkrivera commented May 30, 2024

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/[email protected]
@types/[email protected]
@types/[email protected]
@vitejs/[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Tanstack Query adapter

react-query

TanStack Query version

v5.40.0

TypeScript version

v5.4.5

Additional context

No response

@TkDodo
Copy link
Collaborator

TkDodo commented May 31, 2024

pageParam is unknown because you take away type inference by providing to 2 of the 5 type parameters manually. TypeScript can't do partial type inference yet, so once you define one type parameter, the rest will fall back to the default value.

The best fix is to use type inference all the way through. I've written about this here: https://tkdodo.eu/blog/react-query-and-type-script#infer-all-the-things

and it's also in the docs: https://tanstack.com/query/v5/docs/framework/react/typescript#type-inference

so really, just remove the <> and everything infers nicely:

https://www.typescriptlang.org/play/?ts=5.4.5#code/JYWwDg9gTgLgBAbwFBzgawKYbABShgN2AgFcBnAEQEMYqAaFOcjASQDsAzYN4GDARRIYoATwap2XHn2q1xcQcJEBpDGKQBfOBygQQcAOQABWmzK0AxmgD0+KhZgBaAI5DRBpKEiw4VAB7EZNq6+gb+gR5IGH7e8Nx8UBz2GHA4EOaIjMAAJgBccGwkIABGwowwvAA2GPnmUNwA5ozFENkitTD1bE2ozFAseQVFpVCaSJ5sCUkWKWnmiqKZqGBUDRgAysAAXjVDJWUa40gWEGbwzHMwQQC8cAAUrkr5lwsiAJRw1wB8jMyS3LwBG4RHdkKhUI9RKp2nAANoGSDmMgGOhwSEiAC68ghwIAYmx8qC4Cs1jgqFAqPoNB9vktwah8DASFA2L4AulGPTUAA6NYwAA8l1hGK+dwMAAsYDAwGRctZrAArMinMCVZLiiCVbLCbkwERgYAnbXck4gayIq4oulc8ErCkgWXWm3ggD65nJMEJJIwZPtcEccAAjB8AFRo4Hc72bHbY52oF2VUC8fLoyOrDbbDCxm0abPg6mcrm68UYNh3O74Mg0r5wSvc7I0KhvQu5wvu6oAFVAu0DAAYXb3B7GARUqJUyaTyZT8oHY6r1ZrtVBZFR8phsHhCMRyCvY3yAHLRGATn1TkCEtXmE+osfj9NkVGX4-p32U6tOhkYJksuBPk-c6puhgcU4BrXs4AAfl8So7zWMgANLBpgLgABqIM4HyEg2G1KQMGyFs9y-TciFIMgT1fc97i4KAr3TG8YJPB9fyoWjJ3tatC1QW9GIQoCQJrQNIOg2CMHgwCkJAgNBMw7CMFw7J5ALcZoliRALnSK4NCAA

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@TkDodo TkDodo added invalid This doesn't seem right works as designed types labels May 31, 2024
@pipech
Copy link

pipech commented Jun 11, 2024

@uncvrd
Copy link

uncvrd commented Jun 19, 2024

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

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

No branches or pull requests

4 participants