Skip to content

Commit 982f6ca

Browse files
authored
refactor(types): narrow onSuccess/onError/onMutate/onSettled callback types to Promise<void> | void (#9202)
* refactor: narrow onSuccess/onError/onMutate/onSettled callback types to Promise<void> | void Previously, the `onSuccess`, `onError`, `onMutate`, and `onSettled` callbacks were typed as `() => Promise<unknown> | unknown`. While `unknown` is technically valid, it implies that the return value might be used, assigned, or further processed. However, throughout the codebase, these callbacks are invoked solely for their side effects, and their return values are always ignored. Narrowing the type to `Promise<void> | void` makes this intent explicit, clarifies that any return value will be discarded, and prevents misleading type signatures that suggest otherwise. This commit narrows their types to `() => Promise<void> | void`, which more accurately reflects their intended use. * docs: update callback return types to Promise<void> | void in mutation-related documentation This commit refines the documentation for mutation-related callbacks (`onSuccess`, `onError`, `onSettled`, and `onMutate`), changing their return types from `Promise<unknown> | unknown` to `Promise<void> | void`.
1 parent 7cf6ef5 commit 982f6ca

File tree

8 files changed

+21
-21
lines changed

8 files changed

+21
-21
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?: () => Promise<unknown> | unknown`
217+
- `onSuccess?: () => Promise<void> | 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?: () => Promise<unknown> | unknown`
222+
- `onError?: () => Promise<void> | 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,15 +68,15 @@ 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) => Promise<unknown> | unknown`
71+
- `onSuccess: (data: TData, variables: TVariables, context: TContext) => Promise<void> | void`
7272
- Optional
7373
- This function will fire when the mutation is successful and will be passed the mutation's result.
7474
- If a promise is returned, it will be awaited and resolved before proceeding
75-
- `onError: (err: TError, variables: TVariables, context?: TContext) => Promise<unknown> | unknown`
75+
- `onError: (err: TError, variables: TVariables, context?: TContext) => Promise<void> | void`
7676
- Optional
7777
- This function will fire if the mutation encounters an error and will be passed the error.
7878
- If a promise is returned, it will be awaited and resolved before proceeding
79-
- `onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<unknown> | unknown`
79+
- `onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<void> | void`
8080
- Optional
8181
- This function will fire when the mutation is either successfully fetched or encounters an error and be passed either the data or error
8282
- If a promise is returned, it will be awaited and resolved before proceeding

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) => Promise<unknown> | unknown`
31+
- `onError?: (error: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<void> | 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) => Promise<unknown> | unknown`
35+
- `onSuccess?: (data: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<void> | 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) => Promise<unknown> | unknown`
39+
- `onSettled?: (data: unknown | undefined, error: unknown | null, variables: unknown, context: unknown, mutation: Mutation) => Promise<void> | 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) => Promise<unknown> | unknown`
43+
- `onMutate?: (variables: unknown, mutation: Mutation) => Promise<void> | 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?: () => Promise<unknown> | unknown
24-
onError?: () => Promise<unknown> | unknown
23+
onSuccess?: () => Promise<void> | void
24+
onError?: () => Promise<void> | void
2525
}
2626

2727
/**

packages/query-core/src/mutationCache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ interface MutationCacheConfig {
1616
variables: unknown,
1717
context: unknown,
1818
mutation: Mutation<unknown, unknown, unknown>,
19-
) => Promise<unknown> | unknown
19+
) => Promise<void> | void
2020
onSuccess?: (
2121
data: unknown,
2222
variables: unknown,
2323
context: unknown,
2424
mutation: Mutation<unknown, unknown, unknown>,
25-
) => Promise<unknown> | unknown
25+
) => Promise<void> | void
2626
onMutate?: (
2727
variables: unknown,
2828
mutation: Mutation<unknown, unknown, unknown>,
29-
) => Promise<unknown> | unknown
29+
) => Promise<void> | void
3030
onSettled?: (
3131
data: unknown | undefined,
3232
error: DefaultError | null,
3333
variables: unknown,
3434
context: unknown,
3535
mutation: Mutation<unknown, unknown, unknown>,
36-
) => Promise<unknown> | unknown
36+
) => Promise<void> | void
3737
}
3838

3939
interface NotifyEventMutationAdded extends NotifyEvent {

packages/query-core/src/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface FetchContext<
6767
TData,
6868
TQueryKey extends QueryKey = QueryKey,
6969
> {
70-
fetchFn: () => unknown | Promise<unknown>
70+
fetchFn: () => Promise<unknown> | unknown
7171
fetchOptions?: FetchOptions
7272
signal: AbortSignal
7373
options: QueryOptions<TQueryFnData, TError, TData, any>

packages/query-core/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,18 +1109,18 @@ export interface MutationOptions<
11091109
data: TData,
11101110
variables: TVariables,
11111111
context: TContext,
1112-
) => Promise<unknown> | unknown
1112+
) => Promise<void> | void
11131113
onError?: (
11141114
error: TError,
11151115
variables: TVariables,
11161116
context: TContext | undefined,
1117-
) => Promise<unknown> | unknown
1117+
) => Promise<void> | void
11181118
onSettled?: (
11191119
data: TData | undefined,
11201120
error: TError | null,
11211121
variables: TVariables,
11221122
context: TContext | undefined,
1123-
) => Promise<unknown> | unknown
1123+
) => Promise<void> | void
11241124
retry?: RetryValue<TError>
11251125
retryDelay?: RetryDelayValue<TError>
11261126
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?: () => Promise<unknown> | unknown
15-
onError?: () => Promise<unknown> | unknown
14+
onSuccess?: () => Promise<void> | void
15+
onError?: () => Promise<void> | void
1616
}
1717

1818
export const PersistQueryClientProvider = ({

0 commit comments

Comments
 (0)