Tanstack Query pageParam typescript, readonly
This is my code:
I keep getting the same TS error:
Type 'number' does not satisfy the constraint 'readonly unknown[]'.ts(2344)
⚠ Error (TS2344) |
Type
does not satisfy the constraint readonly unknown[] .
In the Tanstack Query Course I remember they talked about the value that pageParam will always be unknown but we can type it, right?
3 Replies
statutory-emerald•16mo ago
generics for useInfiniteQuery are in this order (TQueryFnData, TError, TData, TQueryKey, TPageParam), you're giving number to querykey type which can only be an array, if you give number to the fifth one then you should be ok
probable-pink•16mo ago
I may be mistaken but I feel like I've seen Dominik suggest against manually typing like that. RQ has some widely impressive inference so you have quite a bit to duplicate
absent-sapphireOP•16mo ago
I think you are right, troywoy. Now i need to understand how to get the value for the current page here:
getNextPageParam: (lastPage) => {
const { pageCount } = lastPage.meta.pagination;
return page < pageCount ? page + 1 : undefined;
},
so i can work with page
100%. It is waaay better not to type and leverage inference