Skip to content

Commit b5ccf9a

Browse files
committed
refactor: standardize use of MaybePromise<T> in place of Promise<T> | T
This commit builds on the changes introduced in [refactor/narrow-callback-types](#9202) and replaces all instances of `Promise<T> | T` with the new `MaybePromise<T>` helper type. The `MaybePromise` type, originally defined in `@tanstack/query-persist-client-core/src/createPersister.ts`, is now moved to `query-core` for broader, consistent use. - Moves the `MaybePromise` type definition to `query-core`, making it the canonical utility for representing values that may be returned synchronously or as a promise. - Updates all relevant callback signatures (such as `onSuccess`, `onError`, `onMutate`, `onSettled`, and other callbacks) to use `MaybePromise<T>` instead of `Promise<T> | T`. - Updates documentation to reference `MaybePromise<T>` for clarity and consistency. This builds on the direction set by [refactor/narrow-callback-types](#9202), further improving type readability and maintainability by using a single, expressive type for all maybe-async callback returns.
1 parent 26a2647 commit b5ccf9a

File tree

11 files changed

+30
-31
lines changed

11 files changed

+30
-31
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<void> | void`
217+
- `onSuccess?: () => MaybePromise<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<void> | void`
222+
- `onError?: () => MaybePromise<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<void> | void`
71+
- `onSuccess: (data: TData, variables: TVariables, context: TContext) => MaybePromise<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<void> | void`
75+
- `onError: (err: TError, variables: TVariables, context?: TContext) => MaybePromise<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<void> | void`
79+
- `onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => MaybePromise<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<void> | void`
31+
- `onError?: (error: unknown, variables: unknown, context: unknown, mutation: Mutation) => MaybePromise<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<void> | void`
35+
- `onSuccess?: (data: unknown, variables: unknown, context: unknown, mutation: Mutation) => MaybePromise<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<void> | void`
39+
- `onSettled?: (data: unknown | undefined, error: unknown | null, variables: unknown, context: unknown, mutation: Mutation) => MaybePromise<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<void> | void`
43+
- `onMutate?: (variables: unknown, mutation: Mutation) => MaybePromise<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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import {
1616
persistQueryClientSubscribe,
1717
} from '@tanstack/query-persist-client-core'
1818
import type { PersistQueryClientOptions as PersistQueryClientOptionsCore } from '@tanstack/query-persist-client-core'
19-
import type { PersistQueryClientFeature } from '@tanstack/angular-query-experimental'
19+
import type { MaybePromise, PersistQueryClientFeature } from '@tanstack/angular-query-experimental'
2020

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

2727
/**

packages/query-async-storage-persister/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { asyncThrottle } from './asyncThrottle'
22
import { noop } from './utils'
33
import type {
44
AsyncStorage,
5-
MaybePromise,
65
PersistedClient,
76
Persister,
87
Promisable,
@@ -29,12 +28,12 @@ interface CreateAsyncStoragePersisterOptions {
2928
* How to serialize the data to storage.
3029
* @default `JSON.stringify`
3130
*/
32-
serialize?: (client: PersistedClient) => MaybePromise<string>
31+
serialize?: (client: PersistedClient) => Promisable<string>
3332
/**
3433
* How to deserialize the data from storage.
3534
* @default `JSON.parse`
3635
*/
37-
deserialize?: (cachedString: string) => MaybePromise<PersistedClient>
36+
deserialize?: (cachedString: string) => Promisable<PersistedClient>
3837

3938
retry?: AsyncPersistRetryer
4039
}

packages/query-core/src/mutationCache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Mutation } from './mutation'
33
import { matchMutation, noop } from './utils'
44
import { Subscribable } from './subscribable'
55
import type { MutationObserver } from './mutationObserver'
6-
import type { DefaultError, MutationOptions, NotifyEvent } from './types'
6+
import type { DefaultError, MaybePromise, MutationOptions, NotifyEvent } from './types'
77
import type { QueryClient } from './queryClient'
88
import type { Action, MutationState } from './mutation'
99
import type { MutationFilters } from './utils'
@@ -16,24 +16,24 @@ interface MutationCacheConfig {
1616
variables: unknown,
1717
context: unknown,
1818
mutation: Mutation<unknown, unknown, unknown>,
19-
) => Promise<void> | void
19+
) => MaybePromise<void>
2020
onSuccess?: (
2121
data: unknown,
2222
variables: unknown,
2323
context: unknown,
2424
mutation: Mutation<unknown, unknown, unknown>,
25-
) => Promise<void> | void
25+
) => MaybePromise<void>
2626
onMutate?: (
2727
variables: unknown,
2828
mutation: Mutation<unknown, unknown, unknown>,
29-
) => Promise<void> | void
29+
) => MaybePromise<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<void> | void
36+
) => MaybePromise<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
@@ -65,7 +65,7 @@ export interface FetchContext<
6565
TData,
6666
TQueryKey extends QueryKey = QueryKey,
6767
> {
68-
fetchFn: () => Promise<unknown> | unknown
68+
fetchFn: () => MaybePromise<unknown>
6969
fetchOptions?: FetchOptions
7070
signal: AbortSignal
7171
options: QueryOptions<TQueryFnData, TError, TData, any>

packages/query-core/src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { QueryFilters, QueryTypeFilter, SkipToken } from './utils'
99
import type { QueryCache } from './queryCache'
1010
import type { MutationCache } from './mutationCache'
1111

12+
export type MaybePromise<T> = T | Promise<T>
1213
export type NonUndefinedGuard<T> = T extends undefined ? never : T
1314

1415
export type DistributiveOmit<
@@ -1105,18 +1106,18 @@ export interface MutationOptions<
11051106
data: TData,
11061107
variables: TVariables,
11071108
context: TContext,
1108-
) => Promise<void> | void
1109+
) => MaybePromise<void>
11091110
onError?: (
11101111
error: TError,
11111112
variables: TVariables,
11121113
context: TContext | undefined,
1113-
) => Promise<void> | void
1114+
) => MaybePromise<void>
11141115
onSettled?: (
11151116
data: TData | undefined,
11161117
error: TError | null,
11171118
variables: TVariables,
11181119
context: TContext | undefined,
1119-
) => Promise<void> | void
1120+
) => MaybePromise<void>
11201121
retry?: RetryValue<TError>
11211122
retryDelay?: RetryDelayValue<TError>
11221123
networkMode?: NetworkMode

packages/query-persist-client-core/src/createPersister.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { matchQuery } from '@tanstack/query-core'
22
import type {
3+
MaybePromise,
34
Query,
45
QueryClient,
56
QueryFilters,
@@ -15,8 +16,6 @@ export interface PersistedQuery {
1516
state: QueryState
1617
}
1718

18-
export type MaybePromise<T> = T | Promise<T>
19-
2019
export interface AsyncStorage<TStorageValue = string> {
2120
getItem: (key: string) => MaybePromise<TStorageValue | undefined | null>
2221
setItem: (key: string, value: TStorageValue) => MaybePromise<unknown>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77
} from '@tanstack/query-persist-client-core'
88
import { IsRestoringProvider, QueryClientProvider } from '@tanstack/react-query'
99
import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core'
10-
import type { OmitKeyof, QueryClientProviderProps } from '@tanstack/react-query'
10+
import type { MaybePromise, OmitKeyof, QueryClientProviderProps } from '@tanstack/react-query'
1111

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

1818
export const PersistQueryClientProvider = ({

packages/svelte-query-persist-client/src/PersistQueryClientProvider.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import type { OmitKeyof, QueryClient } from '@tanstack/svelte-query'
1111
1212
export let client: QueryClient
13-
export let onSuccess: () => Promise<unknown> | unknown = () => undefined
14-
export let onError: () => Promise<unknown> | unknown = () => undefined
13+
export let onSuccess: () => MaybePromise<unknown> = () => undefined
14+
export let onError: () => MaybePromise<unknown> = () => undefined
1515
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
1616
1717
const isRestoring = writable(true)

0 commit comments

Comments
 (0)