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 <office@dorfmeister.cc> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 690fd2a commit fd500a4

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎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+
typeQueryKey= ['dashboard'|'marketing', ...ReadonlyArray<unknown>]
187+
188+
declaremodule'@tanstack/react-query' {
189+
interfaceRegister {
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
exporttypeDefaultError=Registerextends{
@@ -40,7 +42,13 @@ export type DefaultError = Register extends {
4042
? TError
4143
: Error
4244

43-
exporttypeQueryKey=ReadonlyArray<unknown>
45+
exporttypeQueryKey=Registerextends{
46+
queryKey: infer TQueryKey
47+
}
48+
? TQueryKeyextendsArray<unknown>
49+
? TQueryKey
50+
: ReadonlyArray<unknown>
51+
: ReadonlyArray<unknown>
4452

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

999-
exporttypeMutationKey=ReadonlyArray<unknown>
1007+
exporttypeMutationKey=Registerextends{
1008+
mutationKey: infer TMutationKey
1009+
}
1010+
? TMutationKeyextendsArray<unknown>
1011+
? TMutationKey
1012+
: ReadonlyArray<unknown>
1013+
: ReadonlyArray<unknown>
10001014

10011015
exporttypeMutationStatus='idle'|'pending'|'success'|'error'
10021016

0 commit comments

Comments
 (0)