From 7a0f86581b2f10856b4b404eb1dce50a2f1a4247 Mon Sep 17 00:00:00 2001 From: Josiah Nunemaker Date: Wed, 24 Sep 2025 22:05:33 -0400 Subject: [PATCH 1/5] fix(query-core): allow partial query keys in `QueryFilters` and `MutationFilters` Add a type to match tuple prefixes to match partial query keys in `QueryFilters` and `MutationFilters` Fixes tanstack/query#9680 --- packages/query-core/src/types.ts | 10 ++++++++++ packages/query-core/src/utils.ts | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/query-core/src/types.ts b/packages/query-core/src/types.ts index 2c56280e03..526a344d37 100644 --- a/packages/query-core/src/types.ts +++ b/packages/query-core/src/types.ts @@ -36,6 +36,16 @@ export type Override = { export type NoInfer = [T][T extends any ? 0 : never] +export type DropLast> = T extends readonly [ + ...infer R, + unknown, +] + ? R + : never + +export type TuplePrefixes> = + T extends readonly [] ? readonly [] : TuplePrefixes> | T + export interface Register { // defaultError: Error // queryMeta: Record diff --git a/packages/query-core/src/utils.ts b/packages/query-core/src/utils.ts index 2b46cc1c74..e1f8314f12 100644 --- a/packages/query-core/src/utils.ts +++ b/packages/query-core/src/utils.ts @@ -10,6 +10,7 @@ import type { QueryOptions, StaleTime, StaleTimeFunction, + TuplePrefixes, } from './types' import type { Mutation } from './mutation' import type { FetchOptions, Query } from './query' @@ -32,7 +33,7 @@ export interface QueryFilters { /** * Include queries matching this query key */ - queryKey?: TQueryKey + queryKey?: TuplePrefixes /** * Include or exclude stale queries */ @@ -62,7 +63,7 @@ export interface MutationFilters< /** * Include mutations matching this mutation key */ - mutationKey?: MutationKey + mutationKey?: TuplePrefixes /** * Filter by mutation status */ From a2f2b8acaa7cc502323d69d7853b326f9dfa381a Mon Sep 17 00:00:00 2001 From: Josiah Nunemaker Date: Thu, 25 Sep 2025 10:52:48 -0400 Subject: [PATCH 2/5] test(query-core): add tests for partial QueryKey matching for `QueryFilters` add tests to verify that the new partial QueryKey matching for `QueryFilters` does match partial query keys, and also doesn't allow invalid query keys. --- .../query-core/src/__tests__/utils.test-d.tsx | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/query-core/src/__tests__/utils.test-d.tsx b/packages/query-core/src/__tests__/utils.test-d.tsx index 03a7aebad3..3aacc61b81 100644 --- a/packages/query-core/src/__tests__/utils.test-d.tsx +++ b/packages/query-core/src/__tests__/utils.test-d.tsx @@ -1,4 +1,4 @@ -import { describe, expectTypeOf, it } from 'vitest' +import { assertType, describe, expectTypeOf, it } from 'vitest' import { QueryClient } from '../queryClient' import type { QueryFilters } from '../utils' import type { DataTag, QueryKey } from '../types' @@ -35,11 +35,36 @@ describe('QueryFilters', () => { } const queryClient = new QueryClient() - const data = queryClient.getQueryData(a.queryKey!) expectTypeOf(data).toEqualTypeOf() const error = queryClient.getQueryState(a.queryKey!)?.error expectTypeOf(error).toEqualTypeOf() }) + + it('should allow a partial query key to be passed', () => { + const filters: QueryFilters = { + queryKey: ['key'], + } + + expectTypeOf(filters.queryKey).toEqualTypeOf< + | undefined + | readonly [] + | ['key'] + | readonly [ + 'key', + { + a: number + b: string + }, + ] + >() + }) + + it('should error on invalid query keys', () => { + assertType>({ + // @ts-expect-error cannot pass invalid query key + queryKey: ['invalid', { a: 1, b: '1' }], + }) + }) }) From 09c0c10d4a1a1e03d9981928f89092753f9205c4 Mon Sep 17 00:00:00 2001 From: Josiah Nunemaker Date: Thu, 25 Sep 2025 19:56:08 -0400 Subject: [PATCH 3/5] fix(query-core): move tuple prefix helper types to utils.ts prevent tuple helper types from getting exported, and showing up in the public api --- packages/query-core/src/types.ts | 10 ---------- packages/query-core/src/utils.ts | 12 +++++++++++- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/query-core/src/types.ts b/packages/query-core/src/types.ts index 526a344d37..2c56280e03 100644 --- a/packages/query-core/src/types.ts +++ b/packages/query-core/src/types.ts @@ -36,16 +36,6 @@ export type Override = { export type NoInfer = [T][T extends any ? 0 : never] -export type DropLast> = T extends readonly [ - ...infer R, - unknown, -] - ? R - : never - -export type TuplePrefixes> = - T extends readonly [] ? readonly [] : TuplePrefixes> | T - export interface Register { // defaultError: Error // queryMeta: Record diff --git a/packages/query-core/src/utils.ts b/packages/query-core/src/utils.ts index e1f8314f12..1be53edd2d 100644 --- a/packages/query-core/src/utils.ts +++ b/packages/query-core/src/utils.ts @@ -10,13 +10,23 @@ import type { QueryOptions, StaleTime, StaleTimeFunction, - TuplePrefixes, } from './types' import type { Mutation } from './mutation' import type { FetchOptions, Query } from './query' // TYPES +type DropLast> = T extends readonly [ + ...infer R, + unknown, +] + ? R + : never + +type TuplePrefixes> = T extends readonly [] + ? readonly [] + : TuplePrefixes> | T + export interface QueryFilters { /** * Filter to active queries, inactive queries or all queries From 6e8f07554bc499e9a45879f7c4e37c1d2de1de81 Mon Sep 17 00:00:00 2001 From: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com> Date: Fri, 26 Sep 2025 19:47:14 +1000 Subject: [PATCH 4/5] Add changeset --- .changeset/wise-numbers-double.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wise-numbers-double.md diff --git a/.changeset/wise-numbers-double.md b/.changeset/wise-numbers-double.md new file mode 100644 index 0000000000..e195243fb1 --- /dev/null +++ b/.changeset/wise-numbers-double.md @@ -0,0 +1,5 @@ +--- +"@tanstack/query-core": patch +--- + +fix: allow partial query keys in `QueryFilters` From a348b5506e8a6711fceb8a3189908db63c5894c1 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 26 Sep 2025 09:48:37 +0000 Subject: [PATCH 5/5] ci: apply automated fixes --- .changeset/wise-numbers-double.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/wise-numbers-double.md b/.changeset/wise-numbers-double.md index e195243fb1..cd87bca9b1 100644 --- a/.changeset/wise-numbers-double.md +++ b/.changeset/wise-numbers-double.md @@ -1,5 +1,5 @@ --- -"@tanstack/query-core": patch +'@tanstack/query-core': patch --- fix: allow partial query keys in `QueryFilters`