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

TS Issue with queryClient functions.. #8615

Open
amireds opened this issue Feb 6, 2025 · 3 comments
Open

TS Issue with queryClient functions.. #8615

amireds opened this issue Feb 6, 2025 · 3 comments

Comments

@amireds
Copy link

amireds commented Feb 6, 2025

Describe the bug

There's an issue I'm facing using queryClient functions to either invalidate or refetch multiple queries.

According to the docs, Invalidating multiple queries should be done using this syntax:
queryClient.invalidateQueries(['query1', 'query2']):

Result-wise: This works; it refetches the queries, but I get a Typescript error:
Type 'EQueryKeys[][]' has no properties in common with type 'RefetchQueryFilters<any, any, any, any>'.ts(2559)

When I try to correct it into what TS wants which is this syntax:
queryClient.invalidateQueries({
queryKey: ['query1', 'query2']
})

This removes the TS Error, but result-wise: it doesn't invalidate the query.

Pls can this be looked into, or am I the one doing something wrong?

Your minimal, reproducible example

https://codesandbox.io/p/devbox/polished-frog-8vsv92

Steps to reproduce

Try to refetch multiple queiries using

queryClient.refetchQueries(['query1', 'query2']):

Expected behavior

It should refetch the queries, but I'll get a TS error on my IDE

How often does this bug happen?

Every time

Screenshots or Videos

Image

Image

Platform

  • OS: Macbook
  • IDE: VSCode/Cursor

Tanstack Query adapter

react-query

TanStack Query version

v5.64.1

TypeScript version

v5

Additional context

No response

@gs18004
Copy link

gs18004 commented Feb 7, 2025

You should use this like

await queryClient.invalidateQueries({ queryKey: [ ... ] });

docs

Can I take a look at the code sandbox? The page is not found.

@robingullo
Copy link

queryClient.invalidateQueries({ queryKey: ['query1', 'query2'] }) will not invalidate query1 and query2 but queries matching query key ['query1', 'query2']. This probably does not match any of your queries, since I assume their query keys are ['query1'] and ['query2'] (always an array). You probably need:

queryClient.invalidateQueries({ queryKey: ['query1'] })
queryClient.invalidateQueries({ queryKey: ['query2'] })

You can find more on query key matching here : https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation#query-matching-with-invalidatequeries

@amireds
Copy link
Author

amireds commented Feb 7, 2025

You should use this like

await queryClient.invalidateQueries({ queryKey: [ ... ] });
docs

Can I take a look at the code sandbox? The page is not found.

Hi, I've updated the sandbox link

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

No branches or pull requests

3 participants