Skip to content

Commit fd500a4

Browse files
neefrehmanTkDodoautofix-ci[bot]
authored
feat(types): support typing QueryKey and MutationKey via Register (#8521)
* feat(types): support typing QueryKey and MutationKey via Register * ci: apply automated fixes --------- Co-authored-by: Dominik Dorfmeister <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 690fd2a commit fd500a4

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

docs/framework/react/typescript.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,28 @@ declare module '@tanstack/react-query' {
172172
```
173173

174174
[//]: # 'TypingMeta'
175+
[//]: # 'TypingQueryAndMutationKeys'
176+
177+
## Typing query and mutation keys
178+
179+
### Registering the query and mutation key types
180+
181+
Also similarly to registering a [global error type](#registering-a-global-error), you can also register a global `QueryKey` and `MutationKey` type. This allows you to provide more structure to your keys, that matches your application's hierarchy, and have them be typed across all of the library's surface area. Note that the registered type must extend the `Array` type, so that your keys remain an array.
182+
183+
```ts
184+
import '@tanstack/react-query'
185+
186+
type QueryKey = ['dashboard' | 'marketing', ...ReadonlyArray<unknown>]
187+
188+
declare module '@tanstack/react-query' {
189+
interface Register {
190+
queryKey: QueryKey
191+
mutationKey: QueryKey
192+
}
193+
}
194+
```
195+
196+
[//]: # 'TypingQueryAndMutationKeys'
175197
[//]: # 'TypingQueryOptions'
176198

177199
## Typing Query Options

packages/query-core/src/types.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface Register {
3232
// defaultError: Error
3333
// queryMeta: Record<string, unknown>
3434
// mutationMeta: Record<string, unknown>
35+
// queryKey: ReadonlyArray<unknown>
36+
// mutationKey: ReadonlyArray<unknown>
3537
}
3638

3739
export type DefaultError = Register extends {
@@ -40,7 +42,13 @@ export type DefaultError = Register extends {
4042
? TError
4143
: Error
4244

43-
export type QueryKey = ReadonlyArray<unknown>
45+
export type QueryKey = Register extends {
46+
queryKey: infer TQueryKey
47+
}
48+
? TQueryKey extends Array<unknown>
49+
? TQueryKey
50+
: ReadonlyArray<unknown>
51+
: ReadonlyArray<unknown>
4452

4553
export const dataTagSymbol = Symbol('dataTagSymbol')
4654
export type dataTagSymbol = typeof dataTagSymbol
@@ -996,7 +1004,13 @@ export type InfiniteQueryObserverResult<
9961004
| InfiniteQueryObserverLoadingResult<TData, TError>
9971005
| InfiniteQueryObserverPendingResult<TData, TError>
9981006

999-
export type MutationKey = ReadonlyArray<unknown>
1007+
export type MutationKey = Register extends {
1008+
mutationKey: infer TMutationKey
1009+
}
1010+
? TMutationKey extends Array<unknown>
1011+
? TMutationKey
1012+
: ReadonlyArray<unknown>
1013+
: ReadonlyArray<unknown>
10001014

10011015
export type MutationStatus = 'idle' | 'pending' | 'success' | 'error'
10021016

0 commit comments

Comments
 (0)