Skip to content

Commit

Permalink
add types for typed hook results
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Apr 24, 2022
1 parent c5d5335 commit e519a14
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
53 changes: 51 additions & 2 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { useStableQueryArgs } from './useSerializedStableValue'
import type { UninitializedValue } from './constants'
import { UNINITIALIZED_VALUE } from './constants'
import { useShallowStableValue } from './useShallowStableValue'
import { BaseQueryFn } from '../baseQueryTypes'

// Copy-pasted from React-Redux
export const useIsomorphicLayoutEffect =
Expand Down Expand Up @@ -97,7 +98,22 @@ export type UseQuery<D extends QueryDefinition<any, any, any, any>> = <
>(
arg: QueryArgFrom<D> | SkipToken,
options?: UseQuerySubscriptionOptions & UseQueryStateOptions<D, R>
) => UseQueryStateResult<D, R> & ReturnType<UseQuerySubscription<D>>
) => UseQueryHookResult<D, R>

export type UseQueryHookResult<
D extends QueryDefinition<any, any, any, any>,
R = UseQueryStateDefaultResult<D>
> = UseQueryStateResult<D, R> & UseQuerySubscriptionResult<D>

export type TypedUseQueryHookResult<
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn,
R = UseQueryStateDefaultResult<
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
>
> = TypedUseQueryStateResult<ResultType, QueryArg, BaseQuery, R> &
TypedUseQuerySubscriptionResult<ResultType, QueryArg, BaseQuery>

interface UseQuerySubscriptionOptions extends SubscriptionOptions {
/**
Expand Down Expand Up @@ -162,7 +178,19 @@ export type UseQuerySubscription<
> = (
arg: QueryArgFrom<D> | SkipToken,
options?: UseQuerySubscriptionOptions
) => Pick<QueryActionCreatorResult<D>, 'refetch'>
) => UseQuerySubscriptionResult<D>

export type UseQuerySubscriptionResult<
D extends QueryDefinition<any, any, any, any>
> = Pick<QueryActionCreatorResult<D>, 'refetch'>

export type TypedUseQuerySubscriptionResult<
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn
> = UseQuerySubscriptionResult<
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
>

export type UseLazyQueryLastPromiseInfo<
D extends QueryDefinition<any, any, any, any>
Expand Down Expand Up @@ -338,6 +366,15 @@ export type UseQueryStateResult<
R
> = NoInfer<R>

export type TypedUseQueryStateResult<
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn,
R = UseQueryStateDefaultResult<
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
>
> = NoInfer<R>

type UseQueryStateBaseResult<D extends QueryDefinition<any, any, any, any>> =
QuerySubState<D> & {
/**
Expand Down Expand Up @@ -435,6 +472,18 @@ export type UseMutationStateResult<
reset: () => void
}

export type TypedUseMutationResult<
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn,
R = MutationResultSelectorResult<
MutationDefinition<QueryArg, BaseQuery, string, ResultType, string>
>
> = UseMutationStateResult<
MutationDefinition<QueryArg, BaseQuery, string, ResultType, string>,
R
>

/**
* A React hook that lets you trigger an update request for a given endpoint, and subscribes the component to read the request status from the Redux store. The component will re-render as the loading status changes.
*
Expand Down
6 changes: 6 additions & 0 deletions packages/toolkit/src/query/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ const createApi = /* @__PURE__ */ buildCreateApi(
reactHooksModule()
)

export type {
TypedUseQueryHookResult,
TypedUseQueryStateResult,
TypedUseQuerySubscriptionResult,
TypedUseMutationResult,
} from './buildHooks'
export { createApi, reactHooksModule }

0 comments on commit e519a14

Please sign in to comment.