diff --git a/types/index.d.ts b/types/index.d.ts index 8d807f7570..ffcd5f326f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -217,7 +217,7 @@ export interface BaseSharedOptions { suspense: boolean } -export interface BaseQueryOptions { +export interface BaseQueryOptions { /** * Set this to `false` to disable automatic refetching when the query mounts or changes query keys. * To refetch the query, use the `refetch` method returned from the `useQuery` instance. @@ -237,18 +237,16 @@ export interface BaseQueryOptions { refetchIntervalInBackground?: boolean refetchOnWindowFocus?: boolean refetchOnMount?: boolean - onSuccess?: (data: any) => void + onSuccess?: (data: TResult) => void onError?: (err: TError) => void - onSettled?: (data: any, error: TError | null) => void + onSettled?: (data: TResult | undefined, error: TError | null) => void isDataEqual?: (oldData: unknown, newData: unknown) => boolean useErrorBoundary?: boolean } export interface QueryOptions - extends BaseQueryOptions { + extends BaseQueryOptions { suspense?: boolean - onSuccess?: (data: TResult) => void - onSettled?: (data: TResult | undefined, error: TError | null) => void initialData?: TResult | (() => TResult | undefined) initialStale?: boolean | (() => boolean | undefined) } @@ -387,37 +385,57 @@ export interface InfiniteQueryResult ) => Promise | undefined } -export function useMutation( - mutationFn: MutationFunction, - mutationOptions?: MutationOptions +export function useMutation< + TResult, + TVariables = undefined, + TError = Error, + TSnapshot = unknown +>( + mutationFn: MutationFunction, + mutationOptions?: MutationOptions ): [ - MutateFunction, - MutationResult + MutateFunction, + MutationResult ] -export type MutationFunction = ( +export type MutationFunction< + TResult, + TVariables, + TError = Error, + TSnapshot = unknown +> = ( variables: TVariables, - mutateOptions?: MutateOptions -) => Promise + mutateOptions?: MutateOptions +) => Promise -export interface MutateOptions { +export interface MutateOptions< + TResult, + TVariables, + TError = Error, + TSnapshot = unknown +> { onSuccess?: (data: TResult, variables: TVariables) => Promise | void onError?: ( error: TError, - snapshotValue: unknown, - onMutateValue: (variable: TVariables) => Promise | unknown + variables: TVariables, + snapshotValue: TSnapshot ) => Promise | void onSettled?: ( data: undefined | TResult, error: TError | null, - snapshotValue?: unknown + variables: TVariables, + snapshotValue?: TSnapshot ) => Promise | void throwOnError?: boolean } -export interface MutationOptions - extends MutateOptions { - onMutate?: (variables: TVariables) => Promise | unknown +export interface MutationOptions< + TResult, + TVariables, + TError = Error, + TSnapshot = unknown +> extends MutateOptions { + onMutate?: (variables: TVariables) => Promise | TSnapshot useErrorBoundary?: boolean } @@ -605,7 +623,7 @@ export interface QueryCache { ): void isFetching: number subscribe(callback: (queryCache: QueryCache) => void): () => void - clear(): void, + clear(): void resetErrorBoundaries: () => void } @@ -655,18 +673,7 @@ export interface ReactQueryProviderConfig { ) => [string, QueryKeyPart[]] | [] } shared?: BaseSharedOptions - mutations?: { - throwOnError?: boolean - useErrorBoundary?: boolean - onMutate?: (variables: unknown) => Promise | unknown - onSuccess?: (data: unknown, variables?: unknown) => void - onError?: (err: TError, snapshotValue?: unknown) => void - onSettled?: ( - data: unknown | undefined, - error: TError | null, - snapshotValue?: unknown - ) => void - } + mutations?: MutationOptions } export type ConsoleFunction = (...args: any[]) => void diff --git a/types/test.ts b/types/test.ts index 36e889e8e5..b870829ea7 100644 --- a/types/test.ts +++ b/types/test.ts @@ -448,9 +448,14 @@ function globalConfig() { useErrorBoundary: true, onMutate: (variables: unknown) => Promise.resolve(), onSuccess: (data: unknown, variables: unknown) => undefined, - onError: (err: Error, snapshotValue: unknown) => undefined, - onSettled: (data: unknown, error: Error | null, snapshotValue?: any) => + onError: (err: Error, variables: unknown, snapshotValue: unknown) => undefined, + onSettled: ( + data: unknown, + error: Error | null, + variables: unknown, + snapshotValue?: unknown + ) => undefined, }, } }