Skip to content

invalidateQueries predicate option: queryKey property is typed too strictly #8768

@vikingandrobot

Description

@vikingandrobot

Describe the bug

When using the invalidateQueries method of the QueryClient, the queryKey is typed a bit too strictly in the predicate method that we pass to it through the QueryFilters parameter.

When "exact" is false, the queryKey filter is not an exact match, so the queries passed to the predicate option can contain query keys that have a different type from the type of the top-level queryKey. The latest version of the library however types it as the exact same type as the provided queryKey.

In the minimal reproducible example, this is highlighted using a const array of string as query key and the type of the queryKey in the predicate function being the exact same type, even though it could be any other array starting with the same string and then containing any unknown values.

Your minimal, reproducible example

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgRQK4FMoE8DCAbYdAO3gF84AzKCEOAcgAEYBDIgZxYGMBrAeinTNOMALQBHDNjoBuAFCzOEdvG7oscALxwA2nQAmEAOZs6AXThxmbOIuVzevOADkIcTNSiyi6AO4pJuATEMAAUAJQAdMBEAG7MBHrMMOhomIRsIQiyFhKYWADSagBccKpYADTZbgAeQjAlFPFs6JUWYAJ6wJxJ6CUhudhhmgB8iFUWDnAAssyqcGw06KXRenAQFDYAFug8a0RwMNtwAhxrG4dLA+plNgI91qxuUB7jcMAb-QERV4VY2gCM5g0wPoEB83igdCGWQssOO6BgqCg+0aeGacjhpHkcIEiORBygGAxcCxpDCciAA

Steps to reproduce

The Typescript error is visible in the playground and in the errors panel on the right.

Expected behavior

I would expect the type of the queryKey value in the predicate method to only be so strict when the "exact" option is true, or to always be less strict by only stating that it starts with the same values as the provided queryKey in the QueryFilter.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS, Typescript 5.8.2

Tanstack Query adapter

None

TanStack Query version

5.67.1

TypeScript version

5.8.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions