Skip to content

Commit 77b0cab

Browse files
committed
refactor: use union of function signatures for callback return types
Replace callback return type unions with unions of complete function signatures to improve type safety and clarity. This change addresses the issues raised in TanStack#9241 and TanStack#9245 by: - Preventing mixed sync/async behavior within single callbacks - Maintaining support for Promise.all() patterns
1 parent 9f034a9 commit 77b0cab

File tree

7 files changed

+90
-52
lines changed

7 files changed

+90
-52
lines changed

docs/framework/react/plugins/persistQueryClient.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ ReactDOM.createRoot(rootElement).render(
214214

215215
- `persistOptions: PersistQueryClientOptions`
216216
- all [options](#options) you can pass to [persistQueryClient](#persistqueryclient) minus the QueryClient itself
217-
- `onSuccess?: () => void | Promise<void> | Promise<Array<void>>`
217+
- `onSuccess?: (() => void) | (() => Promise<void>)`
218218
- optional
219219
- will be called when the initial restore is finished
220220
- can be used to [resumePausedMutations](../../../../reference/QueryClient.md#queryclientresumepausedmutations)
221221
- if a Promise is returned, it will be awaited; restoring is seen as ongoing until then
222-
- `onError?: () => void | Promise<void> | Promise<Array<void>>`
222+
- `onError?: (() => void) | (() => Promise<void>)`
223223
- optional
224224
- will be called when an error is thrown during restoration
225225
- if a Promise is returned, it will be awaited

docs/framework/react/reference/useMutation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ mutate(variables, {
6868
- This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
6969
- Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
7070
- The value returned from this function will be passed to both the `onError` and `onSettled` functions in the event of a mutation failure and can be useful for rolling back optimistic updates.
71-
- `onSuccess: (data: TData, variables: TVariables, context: TContext) => void | Promise<void> | Promise<Array<void>>`
71+
- `onSuccess: ((data: TData, variables: TVariables, context: TContext) => void) | ((data: TData, variables: TVariables, context: TContext) => Promise<void>)`
7272
- Optional
7373
- This function will fire when the mutation is successful and will be passed the mutation's result.
74-
- `onError: (err: TError, variables: TVariables, context?: TContext) => void | Promise<void> | Promise<Array<void>>`
74+
- `onError: ((err: TError, variables: TVariables, context?: TContext) => void) | ((err: TError, variables: TVariables, context?: TContext) => Promise<void>)`
7575
- Optional
7676
- This function will fire if the mutation encounters an error and will be passed the error.
77-
- `onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => void | Promise<void> | Promise<Array<void>>`
77+
- `onSettled: ((data: TData, error: TError, variables: TVariables, context?: TContext) => void) | ((data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<void>)`
7878
- Optional
7979
- This function will fire when the mutation is either successfully fetched or encounters an error and be passed either the data or error
8080
- `retry: boolean | number | (failureCount: number, error: TError) => boolean`

docs/reference/MutationCache.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ Its available methods are:
2828

2929
**Options**
3030

31-
- `onError?: (error: unknown, variables: unknown, context: unknown, mutation: Mutation) => void | Promise<void> | Promise<Array<void>>`
31+
- `onError?: ((error: unknown, variables: unknown, context: unknown, mutation: Mutation) => void) | ((error: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<void>)`
3232
- Optional
3333
- This function will be called if some mutation encounters an error.
3434
- If you return a Promise from it, it will be awaited
35-
- `onSuccess?: (data: unknown, variables: unknown, context: unknown, mutation: Mutation) => void | Promise<void> | Promise<Array<void>>`
35+
- `onSuccess?: ((data: unknown, variables: unknown, context: unknown, mutation: Mutation) => void) | ((data: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<void>)`
3636
- Optional
3737
- This function will be called if some mutation is successful.
3838
- If you return a Promise from it, it will be awaited
39-
- `onSettled?: (data: unknown | undefined, error: unknown | null, variables: unknown, context: unknown, mutation: Mutation) => void | Promise<void> | Promise<Array<void>>`
39+
- `onSettled?: ((data: unknown | undefined, error: unknown | null, variables: unknown, context: unknown, mutation: Mutation) => void) | ((data: unknown | undefined, error: unknown | null, variables: unknown, context: unknown, mutation: Mutation) => Promise<void>)`
4040
- Optional
4141
- This function will be called if some mutation is settled (either successful or errored).
4242
- If you return a Promise from it, it will be awaited
43-
- `onMutate?: (variables: unknown, mutation: Mutation) => void | Promise<void> | Promise<Array<void>>`
43+
- `onMutate?: ((variables: unknown, mutation: Mutation) => void) | ((variables: unknown, mutation: Mutation) => Promise<void>)`
4444
- Optional
4545
- This function will be called before some mutation executes.
4646
- If you return a Promise from it, it will be awaited

packages/angular-query-persist-client/src/with-persist-query-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import type { PersistQueryClientFeature } from '@tanstack/angular-query-experime
2020

2121
type PersistQueryClientOptions = {
2222
persistOptions: Omit<PersistQueryClientOptionsCore, 'queryClient'>
23-
onSuccess?: () => void | Promise<void> | Promise<Array<void>>
24-
onError?: () => void | Promise<void> | Promise<Array<void>>
23+
onSuccess?: (() => void) | (() => Promise<void>)
24+
onError?: (() => void) | (() => Promise<void>)
2525
}
2626

2727
/**

packages/query-core/src/mutationCache.ts

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,56 @@ import type { MutationFilters } from './utils'
1111
// TYPES
1212

1313
interface MutationCacheConfig {
14-
onError?: (
15-
error: DefaultError,
16-
variables: unknown,
17-
context: unknown,
18-
mutation: Mutation<unknown, unknown, unknown>,
19-
) => void | Promise<void> | Promise<Array<void>>
20-
onSuccess?: (
21-
data: unknown,
22-
variables: unknown,
23-
context: unknown,
24-
mutation: Mutation<unknown, unknown, unknown>,
25-
) => void | Promise<void> | Promise<Array<void>>
26-
onMutate?: (
27-
variables: unknown,
28-
mutation: Mutation<unknown, unknown, unknown>,
29-
) => void | Promise<void> | Promise<Array<void>>
30-
onSettled?: (
31-
data: unknown | undefined,
32-
error: DefaultError | null,
33-
variables: unknown,
34-
context: unknown,
35-
mutation: Mutation<unknown, unknown, unknown>,
36-
) => void | Promise<void> | Promise<Array<void>>
14+
onError?:
15+
| ((
16+
error: DefaultError,
17+
variables: unknown,
18+
context: unknown,
19+
mutation: Mutation<unknown, unknown, unknown>,
20+
) => void)
21+
| ((
22+
error: DefaultError,
23+
variables: unknown,
24+
context: unknown,
25+
mutation: Mutation<unknown, unknown, unknown>,
26+
) => Promise<void>)
27+
onSuccess?:
28+
| ((
29+
data: unknown,
30+
variables: unknown,
31+
context: unknown,
32+
mutation: Mutation<unknown, unknown, unknown>,
33+
) => void)
34+
| ((
35+
data: unknown,
36+
variables: unknown,
37+
context: unknown,
38+
mutation: Mutation<unknown, unknown, unknown>,
39+
) => Promise<void>)
40+
onMutate?:
41+
| ((
42+
variables: unknown,
43+
mutation: Mutation<unknown, unknown, unknown>,
44+
) => void)
45+
| ((
46+
variables: unknown,
47+
mutation: Mutation<unknown, unknown, unknown>,
48+
) => Promise<void>)
49+
onSettled?:
50+
| ((
51+
data: unknown | undefined,
52+
error: DefaultError | null,
53+
variables: unknown,
54+
context: unknown,
55+
mutation: Mutation<unknown, unknown, unknown>,
56+
) => void)
57+
| ((
58+
data: unknown | undefined,
59+
error: DefaultError | null,
60+
variables: unknown,
61+
context: unknown,
62+
mutation: Mutation<unknown, unknown, unknown>,
63+
) => Promise<void>)
3764
}
3865

3966
interface NotifyEventMutationAdded extends NotifyEvent {

packages/query-core/src/types.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,22 +1102,33 @@ export interface MutationOptions<
11021102
onMutate?: (
11031103
variables: TVariables,
11041104
) => Promise<TContext | undefined> | TContext | undefined
1105-
onSuccess?: (
1106-
data: TData,
1107-
variables: TVariables,
1108-
context: TContext,
1109-
) => void | Promise<void> | Promise<Array<void>>
1110-
onError?: (
1111-
error: TError,
1112-
variables: TVariables,
1113-
context: TContext | undefined,
1114-
) => void | Promise<void> | Promise<Array<void>>
1115-
onSettled?: (
1116-
data: TData | undefined,
1117-
error: TError | null,
1118-
variables: TVariables,
1119-
context: TContext | undefined,
1120-
) => void | Promise<void> | Promise<Array<void>>
1105+
onSuccess?:
1106+
| ((data: TData, variables: TVariables, context: TContext) => void)
1107+
| ((data: TData, variables: TVariables, context: TContext) => Promise<void>)
1108+
onError?:
1109+
| ((
1110+
error: TError,
1111+
variables: TVariables,
1112+
context: TContext | undefined,
1113+
) => void)
1114+
| ((
1115+
error: TError,
1116+
variables: TVariables,
1117+
context: TContext | undefined,
1118+
) => Promise<void>)
1119+
onSettled?:
1120+
| ((
1121+
data: TData | undefined,
1122+
error: TError | null,
1123+
variables: TVariables,
1124+
context: TContext | undefined,
1125+
) => void)
1126+
| ((
1127+
data: TData | undefined,
1128+
error: TError | null,
1129+
variables: TVariables,
1130+
context: TContext | undefined,
1131+
) => Promise<void>)
11211132
retry?: RetryValue<TError>
11221133
retryDelay?: RetryDelayValue<TError>
11231134
networkMode?: NetworkMode

packages/react-query-persist-client/src/PersistQueryClientProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import type { OmitKeyof, QueryClientProviderProps } from '@tanstack/react-query'
1111

1212
export type PersistQueryClientProviderProps = QueryClientProviderProps & {
1313
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
14-
onSuccess?: () => void | Promise<void> | Promise<Array<void>>
15-
onError?: () => void | Promise<void> | Promise<Array<void>>
14+
onSuccess?: (() => void) | (() => Promise<void>)
15+
onError?: (() => void) | (() => Promise<void>)
1616
}
1717

1818
export const PersistQueryClientProvider = ({

0 commit comments

Comments
 (0)