Skip to content

Commit ca3a56a

Browse files
committed
fix(UseMutationResult): MutationState type should be more specific, to allow narrowing to match useQuery
fix(UseMutationResult): unify result type between core and react fix(UseMutationResult): revert back mutateAsync to core/types fix(mutationObserver): updateResult should declare a type then cast fix(useMutation): revert some changes fix(useMutation): revert uneeded changes fix(useMutation): use Exclude instead of Omit to override mutate to return the correct union type
1 parent 1427b1e commit ca3a56a

File tree

3 files changed

+100
-18
lines changed

3 files changed

+100
-18
lines changed

src/core/mutationObserver.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { QueryClient } from './queryClient'
44
import { Subscribable } from './subscribable'
55
import type {
66
MutateOptions,
7+
MutationObserverBaseResult,
78
MutationObserverResult,
89
MutationObserverOptions,
910
} from './types'
@@ -129,7 +130,12 @@ export class MutationObserver<
129130
? this.currentMutation.state
130131
: getDefaultState<TData, TError, TVariables, TContext>()
131132

132-
this.currentResult = {
133+
const result: MutationObserverBaseResult<
134+
TData,
135+
TError,
136+
TVariables,
137+
TContext
138+
> = {
133139
...state,
134140
isLoading: state.status === 'loading',
135141
isSuccess: state.status === 'success',
@@ -138,6 +144,13 @@ export class MutationObserver<
138144
mutate: this.mutate,
139145
reset: this.reset,
140146
}
147+
148+
this.currentResult = result as MutationObserverResult<
149+
TData,
150+
TError,
151+
TVariables,
152+
TContext
153+
>
141154
}
142155

143156
private notify(options: NotifyOptions) {

src/core/types.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ export type MutateFunction<
564564
options?: MutateOptions<TData, TError, TVariables, TContext>
565565
) => Promise<TData>
566566

567-
export interface MutationObserverResult<
567+
export interface MutationObserverBaseResult<
568568
TData = unknown,
569569
TError = unknown,
570570
TVariables = void,
@@ -578,6 +578,77 @@ export interface MutationObserverResult<
578578
reset: () => void
579579
}
580580

581+
export interface MutationObserverIdleResult<
582+
TData = unknown,
583+
TError = unknown,
584+
TVariables = void,
585+
TContext = unknown
586+
> extends MutationObserverBaseResult<TData, TError, TVariables, TContext> {
587+
data: undefined
588+
error: null
589+
isError: false
590+
isIdle: true
591+
isLoading: false
592+
isSuccess: false
593+
status: 'idle'
594+
}
595+
596+
export interface MutationObserverLoadingResult<
597+
TData = unknown,
598+
TError = unknown,
599+
TVariables = void,
600+
TContext = unknown
601+
> extends MutationObserverBaseResult<TData, TError, TVariables, TContext> {
602+
data: undefined
603+
error: null
604+
isError: false
605+
isIdle: false
606+
isLoading: true
607+
isSuccess: false
608+
status: 'loading'
609+
}
610+
611+
export interface MutationObserverErrorResult<
612+
TData = unknown,
613+
TError = unknown,
614+
TVariables = void,
615+
TContext = unknown
616+
> extends MutationObserverBaseResult<TData, TError, TVariables, TContext> {
617+
data: undefined
618+
error: TError
619+
isError: true
620+
isIdle: false
621+
isLoading: false
622+
isSuccess: false
623+
status: 'error'
624+
}
625+
626+
export interface MutationObserverSuccessResult<
627+
TData = unknown,
628+
TError = unknown,
629+
TVariables = void,
630+
TContext = unknown
631+
> extends MutationObserverBaseResult<TData, TError, TVariables, TContext> {
632+
data: TData
633+
error: null
634+
isError: false
635+
isIdle: false
636+
isLoading: false
637+
isSuccess: true
638+
status: 'success'
639+
}
640+
641+
export type MutationObserverResult<
642+
TData = unknown,
643+
TError = unknown,
644+
TVariables = void,
645+
TContext = unknown
646+
> =
647+
| MutationObserverIdleResult<TData, TError, TVariables, TContext>
648+
| MutationObserverLoadingResult<TData, TError, TVariables, TContext>
649+
| MutationObserverErrorResult<TData, TError, TVariables, TContext>
650+
| MutationObserverSuccessResult<TData, TError, TVariables, TContext>
651+
581652
export interface DefaultOptions<TError = unknown> {
582653
queries?: QueryObserverOptions<unknown, TError>
583654
mutations?: MutationObserverOptions<unknown, TError, unknown, unknown>

src/react/types.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { RetryValue, RetryDelayValue } from '../core/retryer'
22
import {
33
InfiniteQueryObserverOptions,
44
InfiniteQueryObserverResult,
5-
MutateOptions,
6-
MutationStatus,
5+
MutationObserverResult,
76
MutationKey,
87
QueryObserverOptions,
98
QueryObserverResult,
109
QueryKey,
1110
MutationFunction,
11+
MutateOptions,
1212
} from '../core/types'
1313

1414
export interface UseBaseQueryOptions<
@@ -119,24 +119,22 @@ export type UseMutateAsyncFunction<
119119
options?: MutateOptions<TData, TError, TVariables, TContext>
120120
) => Promise<TData>
121121

122-
export interface UseMutationResult<
122+
export type UseBaseMutationResult<
123123
TData = unknown,
124124
TError = unknown,
125125
TVariables = unknown,
126126
TContext = unknown
127-
> {
128-
context: TContext | undefined
129-
data: TData | undefined
130-
error: TError | null
131-
failureCount: number
132-
isError: boolean
133-
isIdle: boolean
134-
isLoading: boolean
135-
isPaused: boolean
136-
isSuccess: boolean
127+
> = Exclude<
128+
MutationObserverResult<TData, TError, TVariables, TContext>,
129+
'mutate'
130+
> & {
137131
mutate: UseMutateFunction<TData, TError, TVariables, TContext>
138132
mutateAsync: UseMutateAsyncFunction<TData, TError, TVariables, TContext>
139-
reset: () => void
140-
status: MutationStatus
141-
variables: TVariables | undefined
142133
}
134+
135+
export type UseMutationResult<
136+
TData = unknown,
137+
TError = unknown,
138+
TVariables = unknown,
139+
TContext = unknown
140+
> = UseBaseMutationResult<TData, TError, TVariables, TContext>

0 commit comments

Comments
 (0)