Skip to content

Commit

Permalink
Fix types for lazy query :fingers_crossed:
Browse files Browse the repository at this point in the history
  • Loading branch information
msutkowski committed Nov 6, 2021
1 parent 5a48ee9 commit 0cdf5e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
27 changes: 25 additions & 2 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type StartQueryActionCreator<
options?: StartQueryActionCreatorOptions
) => ThunkAction<QueryActionCreatorResult<D>, any, any, AnyAction>

export type QueryActionCreatorResult<
type CommonQueryActionCreatorResult<
D extends QueryDefinition<any, any, any, any>
> = Promise<QuerySubState<D>> & {
> = {
arg: QueryArgFrom<D>
requestId: string
subscriptionOptions: SubscriptionOptions | undefined
Expand All @@ -58,6 +58,29 @@ export type QueryActionCreatorResult<
updateSubscriptionOptions(options: SubscriptionOptions): void
}

export type QueryActionCreatorResult<
D extends QueryDefinition<any, any, any, any>
> = Promise<QuerySubState<D>> & CommonQueryActionCreatorResult<D>

export type LazyQueryActionCreatorResult<
D extends QueryDefinition<any, any, any, any>
> = Promise<
| { data: ResultTypeFrom<D> }
| {
error:
| Exclude<
BaseQueryError<
D extends QueryDefinition<any, infer BaseQuery, any, any>
? BaseQuery
: never
>,
undefined
>
| SerializedError
}
> &
CommonQueryActionCreatorResult<D>

type StartMutationActionCreator<
D extends MutationDefinition<any, any, any, any>
> = (
Expand Down
9 changes: 5 additions & 4 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
import type {
QueryActionCreatorResult,
MutationActionCreatorResult,
LazyQueryActionCreatorResult,
} from '@reduxjs/toolkit/dist/query/core/buildInitiate'
import type { SerializeQueryArgs } from '@reduxjs/toolkit/dist/query/defaultSerializeQueryArgs'
import { shallowEqual } from 'react-redux'
Expand Down Expand Up @@ -220,7 +221,7 @@ export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> = {
(
arg: QueryArgFrom<D>,
preferCacheValue?: boolean
): QueryActionCreatorResult<D>
): LazyQueryActionCreatorResult<D>
}

/**
Expand Down Expand Up @@ -670,7 +671,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
const dispatch = useDispatch<ThunkDispatch<any, any, AnyAction>>()

const [arg, setArg] = useState<any>(UNINITIALIZED_VALUE)
const promiseRef = useRef<QueryActionCreatorResult<any> | undefined>()
const promiseRef = useRef<LazyQueryActionCreatorResult<any> | undefined>()

const stableSubscriptionOptions = useShallowStableValue({
refetchOnReconnect,
Expand All @@ -695,7 +696,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({

const trigger = useCallback(
function (arg: any, preferCacheValue = false) {
let promise: QueryActionCreatorResult<any>
let promise: LazyQueryActionCreatorResult<any>

batch(() => {
promiseRef.current?.unsubscribe()
Expand All @@ -705,7 +706,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
subscriptionOptions: subscriptionOptionsRef.current,
forceRefetch: !preferCacheValue,
})
)
) as any

setArg(arg)
})
Expand Down
3 changes: 0 additions & 3 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,6 @@ describe('hooks tests', () => {
| {
error: { status: number; data: unknown } | SerializedError
}
| {
data: undefined
}
| {
data: {
name: string
Expand Down

0 comments on commit 0cdf5e8

Please sign in to comment.