T
TanStack3y ago
other-emerald

Getting type error when using string to invalidate queries

Hey, I am following the invalidation query docs to invalidate all queries with the term rates as the first element in the query key array by using
queryClient.invalidateQueries('rates');
queryClient.invalidateQueries('rates');
I am using the useQueryClient hook to get the queryClient. This clears the queries I want to clear, but I am getting a type error when I use the string:
No overload matches this call.
Overload 1 of 2, '(filters?: InvalidateQueryFilters<unknown> | undefined, options?: InvalidateOptions | undefined): Promise<void>', gave the following error.
Type '"rates"' has no properties in common with type 'InvalidateQueryFilters<unknown>'.
Overload 2 of 2, '(queryKey?: QueryKey | undefined, filters?: InvalidateQueryFilters<unknown> | undefined, options?: InvalidateOptions | undefined): Promise<void>', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'readonly unknown[]'.ts(2769)
No overload matches this call.
Overload 1 of 2, '(filters?: InvalidateQueryFilters<unknown> | undefined, options?: InvalidateOptions | undefined): Promise<void>', gave the following error.
Type '"rates"' has no properties in common with type 'InvalidateQueryFilters<unknown>'.
Overload 2 of 2, '(queryKey?: QueryKey | undefined, filters?: InvalidateQueryFilters<unknown> | undefined, options?: InvalidateOptions | undefined): Promise<void>', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'readonly unknown[]'.ts(2769)
if I try putting it in an array:
queryClient.invalidateQueries(['rates']);
queryClient.invalidateQueries(['rates']);
the type error goes away but the query invalidation does not work. How can i fix this? Thanks
3 Replies
xenophobic-harlequin
xenophobic-harlequin3y ago
queryKeys need to be arrays
other-emerald
other-emeraldOP3y ago
Hey. Why would the plain string clear the query but not the array though?
xenophobic-harlequin
xenophobic-harlequin3y ago
invalidation doesn't "clear". It's best if you'd show a codesandbox reproduction

Did you find this page helpful?