feat(@tanstack/query-core): make query key functions accept optional …#3925
feat(@tanstack/query-core): make query key functions accept optional …#3925AustinRobinson wants to merge 5 commits into
Conversation
…partial options This change allows query key functions to be called with no arguments, partial arguments, or full arguments, enabling type-safe prefix matching for query invalidation with TanStack Query's exact: false option. - Changed options parameter from required to optional - Wrapped options type in Partial<> to allow partial arguments - Options functions remain strict to preserve type safety for queries - Backward compatible: existing code passing full options continues to work Resolves hey-api#980, hey-api#1682, hey-api#3789
|
|
|
@AustinRobinson is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
🦋 Changeset detectedLatest commit: 17bd9da The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Important
This PR introduces a type error for infinite query key functions. The createQueryKey utility needs its parameter type updated to match the new partial query key signatures.
Reviewed changes — make generated TanStack Query query key functions accept optional partial options for prefix matching.
- Change query key parameter type — from
options: Options<T>tooptions?: Partial<Options<T>>inqueryKeyStatement - Remove unused
hasOperationDataRequiredimport — no longer needed after the parameter change
⚠️ Infinite query key functions will fail to typecheck
The queryKeyStatement change makes query key functions pass Partial<Options<T>> to createQueryKey, but createQueryKey still declares its parameter as options?: TOptions. For infinite queries, the explicit return type QueryKey<Options<T>> creates a mismatch: TypeScript infers TOptions = Partial<Options<T>> from the argument, producing return type QueryKey<Partial<Options<T>>>, which is not assignable to the annotated QueryKey<Options<T>>.
Technical details
# Infinite query key functions will fail to typecheck
## Affected sites
- `packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts:49` — `createQueryKeyFunction` parameter type
## Required outcome
- Infinite query key functions must compile without type errors after the PR
- Regular query key functions must continue to work as intended
## Suggested approach
Update `createQueryKeyFunction` in `queryKey.ts` line 49:
```typescript
// Before
.param('options', (p) => p.optional().type(TOptionsType))
// After
.param('options', (p) => p.optional().type(`Partial<${TOptionsType}>`))
```
This allows `createQueryKey` to accept partial options while inferring the full `TOptions` type from contextual return types (e.g., the explicit `QueryKey<Options<TData>>` on infinite query key functions).Kimi K2 | 𝕏
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3925 +/- ##
==========================================
- Coverage 38.77% 38.77% -0.01%
==========================================
Files 595 595
Lines 21365 21367 +2
Branches 6298 6298
==========================================
Hits 8284 8284
- Misses 10659 10661 +2
Partials 2422 2422
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@hey-api/codegen-core
@hey-api/json-schema-ref-parser
@hey-api/nuxt
@hey-api/openapi-ts
@hey-api/shared
@hey-api/spec-types
@hey-api/types
@hey-api/vite-plugin
commit: |
|
@mrlubos Just wanted to follow up on this PR and see whether it’s something you’d be interested in merging. If there are any changes, adjustments, or additional context you’d like before reviewing, I’d be happy to update it. Thanks! |

Make Query Key Functions Accept Optional Partial Options for TanStack Query Prefix Matching
Changes Made
Modified
packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.tsline 165:This changes query key function signatures from:
to:
Options functions remain strict to preserve type safety for queries.
Problem
When using TanStack Query's prefix matching feature for query invalidation (via
exact: false), the generated query key functions require all parameters to be provided, making it difficult to invalidate queries by prefix without passing unnecessary parameters or resorting to manual string literals.Issues:
Motivation
After mutations that affect multiple related queries, I need to invalidate all queries for a specific endpoint regardless of their parameters:
Why this matters:
Proposed Solution
Make query key functions accept optional partial options (
options?: Partial<Options<T>>) while keeping options functions strict for type safety.Key changes:
optionsparameter optional (add?)optionstype partial (wrap inPartial<>)This allows calling the function with no arguments, partial arguments, or full arguments.
Current Generated Code
Proposed Change
Why This Works
Type Safety is Preserved for Queries
The options function (used with
useQuery) remains strict:Flexibility for Invalidation
The query key function accepts no arguments, partial arguments, or full arguments:
Backward Compatible
Existing code passing full options continues to work (full type is assignable to
Partial<T>).Benefits
listTodosQueryKey()Alternative Considered
Exporting Operation ID Constants
Instead of making query key functions accept partial options, we could export constants for each operation ID:
Usage:
Why this is less ideal:
While this approach would solve the type safety issue, it has several drawbacks:
createQueryKeystructures the query key with_id[{ _id: ... }]formatlistTodosQueryKey()The proposed solution is superior because it:
Testing
Snapshot Tests
The snapshot tests show expected failures because the generated query key function signatures have changed. The snapshots will need to be updated with
pnpm tu @test/openapi-ts-tanstack-query-v5to reflect the newoptions?: Partial<...>signatures.Related Issues
Resolves #980, #1682, #3789
Configuration (Optional)
If desired, this could be made configurable in the future: