Skip to content

Commit a7891b9

Browse files
fix(query-core): mutation meta typing (TanStack#6680)
* fix QueryMeta type * reverting previous fix and applied similar behaviour to MutationMeta * added behaviour details in the documentation
1 parent 96cdb28 commit a7891b9

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

docs/react/typescript.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,15 @@ const { error } = useQuery({ queryKey: ['groups'], queryFn: fetchGroups })
157157

158158
### Registering global Meta
159159

160-
Similarly to registering a [global error type](#registering-a-global-error) you can also register a global `Meta` type. This ensures the optional `meta` field on [queries](./reference/useQuery.md) and [mutations](./reference/useMutation.md) stays consistent and is type-safe.
160+
Similarly to registering a [global error type](#registering-a-global-error) you can also register a global `Meta` type. This ensures the optional `meta` field on [queries](./reference/useQuery.md) and [mutations](./reference/useMutation.md) stays consistent and is type-safe. Note that the registered type must extend `Record<string, unknown>` so that `meta` remains an object.
161161

162162
```ts
163163
import '@tanstack/react-query'
164164

165+
interface MyMeta extends Record<string, unknown> {
166+
// Your meta type definition.
167+
}
168+
165169
declare module '@tanstack/react-query' {
166170
interface Register {
167171
queryMeta: MyMeta

packages/query-core/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,9 @@ export type MutationStatus = 'idle' | 'pending' | 'success' | 'error'
688688
export type MutationMeta = Register extends {
689689
mutationMeta: infer TMutationMeta
690690
}
691-
? TMutationMeta
691+
? TMutationMeta extends Record<string, unknown>
692+
? TMutationMeta
693+
: Record<string, unknown>
692694
: Record<string, unknown>
693695

694696
export type MutationFunction<TData = unknown, TVariables = unknown> = (

0 commit comments

Comments
 (0)