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

Method getQueriesData does not return correct type #8613

Open
U-OK opened this issue Feb 6, 2025 · 4 comments · May be fixed by #8614
Open

Method getQueriesData does not return correct type #8613

U-OK opened this issue Feb 6, 2025 · 4 comments · May be fixed by #8614

Comments

@U-OK
Copy link

U-OK commented Feb 6, 2025

Describe the bug

Method getQueriesData does not return correct type. I tried to use it with queryOptions or infiniteQueryOptions, but returned type is always unknown.

Your minimal, reproducible example

https://www.typescriptlang.org/play/?ssl=20&ssc=30&pln=20&pc=44#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ4A0c2ueAwgDbDoB2MRAjjvgPJgzAQ2pwC+cAMygQQcAOQABGAENuMgMYBrAPRR00+TAC0TUmICwAKCPyuqeLvyVqdOAF44NdAHdizclVowAFAEojJmbw8tKw2LIcMHhsHGb2cJbR7Jzc3ghGcAnuANLoeABccADaAEQhsAD6TBHAUSUAugQZWaQAYjSF0qh4NPJwfvYAfIjNmWowyFA0cCUgeABq0hQ4Jc28TYa8-saGpnIt+PFoWO7e5TDhdLVJsdzbRokAdAAm0jJGysqZmQB6APwBXZBOCvGQASRoAlwame8US1i8jwA5ugLu4ACJvaRnUJompRGIpVCPRK5PDbT7fOD-QF7cwgrEAFXQ9Ic8M8dGRqJIUGoqExMjSpLyhXOl0iNyJJJyeS2Hy+33+QA

Steps to reproduce

Check dataTest variable type. It's incorrect

Expected behavior

Type shold be like dataInferred vatiable, but not unknown.

How often does this bug happen?

None

Screenshots or Videos

Image

Platform

Any platform

Tanstack Query adapter

react-query

TanStack Query version

5.28.13

TypeScript version

5.7.3

Additional context

No response

@michael-harvey
Copy link

This situation is mentioned in the docs

https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientgetqueriesdata

Caveats

Because the returned data in each tuple can be of varying structures (i.e. using a filter to return "active" queries can return different data types), the TData generic defaults to unknown. If you provide a more specific type to TData it is assumed that you are certain each tuple's data entry is all the same type.

This distinction is more a "convenience" for ts devs that know which structure will be returned.

So you could type the data by doing something like queryClient.getQueriesData<string>({ ...})

@U-OK
Copy link
Author

U-OK commented Feb 6, 2025

This situation is mentioned in the docs

https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientgetqueriesdata

Caveats
Because the returned data in each tuple can be of varying structures (i.e. using a filter to return "active" queries can return different data types), the TData generic defaults to unknown. If you provide a more specific type to TData it is assumed that you are certain each tuple's data entry is all the same type.
This distinction is more a "convenience" for ts devs that know which structure will be returned.

So you could type the data by doing something like queryClient.getQueriesData<string>({ ...})

Yes, it is. But this mentioned is pretty old and being writed before we get queryOptions or inifiniteQueryOptions. It's strictly key may garanty for us, the touple will be exactly the same type, as returned from TaggetQueryKey type.

UPD.
If no one minds, then here is my proposal to fix the problems: #8614

@TkDodo
Copy link
Collaborator

TkDodo commented Feb 6, 2025

getQueriesData is fuzzy, so even if you pass a key from queryOptions, there is no guarantee that the results will have only that type. You could have an entry with a different type that matches the key filter e.g. because it starts with the same key.

@U-OK
Copy link
Author

U-OK commented Feb 7, 2025

@TkDodo
Yes, that's right. Didn't think about it. At the same time, the getQueriesData method has an exact field. What if I extend the type so that if exact: true is present and queryOptions is passed, the correct type is returned? This would solve the problem, it seems to me.

UPD.
I sended another commit to PR for my idea reference

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

Successfully merging a pull request may close this issue.

3 participants