Skip to content

Commit

Permalink
add more Typed wrappers and make sure they're all exported (#4866)
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 authored Mar 7, 2025
1 parent a59f90c commit 642d795
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 2 deletions.
150 changes: 148 additions & 2 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,22 @@ export type LazyInfiniteQueryTrigger<
): InfiniteQueryActionCreatorResult<D>
}

export type TypedLazyInfiniteQueryTrigger<
ResultType,
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
> = LazyInfiniteQueryTrigger<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>
>

interface UseInfiniteQuerySubscriptionOptions<
D extends InfiniteQueryDefinition<any, any, any, any, any>,
> extends SubscriptionOptions {
Expand Down Expand Up @@ -890,6 +906,36 @@ export type InfiniteQueryStateSelector<
D extends InfiniteQueryDefinition<any, any, any, any, any>,
> = (state: UseInfiniteQueryStateDefaultResult<D>) => R

export type TypedInfiniteQueryStateSelector<
ResultType,
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
SelectedResult extends Record<
string,
any
> = UseInfiniteQueryStateDefaultResult<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>
>,
> = InfiniteQueryStateSelector<
SelectedResult,
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>
>

/**
* A React hook that automatically triggers fetches of data from an endpoint, 'subscribes' the component to the cached data, and reads the request status and cached data from the Redux store. The component will re-render as the loading status changes and the data becomes available. Additionally, it will cache multiple "pages" worth of responses within a single cache entry, and allows fetching more pages forwards and backwards from the current cached pages.
*
Expand Down Expand Up @@ -927,6 +973,22 @@ export type UseInfiniteQuery<
'fetchNextPage' | 'fetchPreviousPage'
>

export type TypedUseInfiniteQuery<
ResultType,
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
> = UseInfiniteQuery<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>
>

/**
* A React hook that reads the request status and cached data from the Redux store. The component will re-render as the loading status changes and the data becomes available.
*
Expand Down Expand Up @@ -987,6 +1049,33 @@ export type UseInfiniteQueryHookResult<
> = UseInfiniteQueryStateResult<D, R> &
Pick<UseInfiniteQuerySubscriptionResult<D>, 'refetch'>

export type TypedUseInfiniteQueryHookResult<
ResultType,
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
R extends Record<string, any> = UseInfiniteQueryStateDefaultResult<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>
>,
> = UseInfiniteQueryHookResult<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>,
R
>

export type UseInfiniteQueryStateOptions<
D extends InfiniteQueryDefinition<any, any, any, any, any>,
R extends Record<string, any>,
Expand Down Expand Up @@ -1057,11 +1146,68 @@ export type UseInfiniteQueryStateOptions<
selectFromResult?: InfiniteQueryStateSelector<R, D>
}

export type TypedUseInfiniteQueryStateOptions<
ResultType,
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
SelectedResult extends Record<
string,
any
> = UseInfiniteQueryStateDefaultResult<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>
>,
> = UseInfiniteQueryStateOptions<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>,
SelectedResult
>

export type UseInfiniteQueryStateResult<
_ extends InfiniteQueryDefinition<any, any, any, any, any>,
R,
D extends InfiniteQueryDefinition<any, any, any, any, any>,
R = UseInfiniteQueryStateDefaultResult<D>,
> = TSHelpersNoInfer<R>

export type TypedUseInfiniteQueryStateResult<
ResultType,
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
R = UseInfiniteQueryStateDefaultResult<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>
>,
> = UseInfiniteQueryStateResult<
InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery,
string,
ResultType,
string
>,
R
>

type UseInfiniteQueryStateBaseResult<
D extends InfiniteQueryDefinition<any, any, any, any, any>,
> = InfiniteQuerySubState<D> & {
Expand Down
9 changes: 9 additions & 0 deletions packages/toolkit/src/query/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export type {
TypedUseLazyQuerySubscription,
TypedUseQueryStateOptions,
TypedUseLazyQueryStateResult,
TypedUseInfiniteQuery,
TypedUseInfiniteQueryHookResult,
TypedUseInfiniteQueryStateResult,
TypedUseInfiniteQuerySubscriptionResult,
TypedUseInfiniteQueryStateOptions,
TypedInfiniteQueryStateSelector,
TypedUseInfiniteQuerySubscription,
TypedUseInfiniteQueryState,
TypedLazyInfiniteQueryTrigger,
} from './buildHooks'
export { UNINITIALIZED_VALUE } from './constants'
export { createApi, reactHooksModule, reactHooksModuleName }

0 comments on commit 642d795

Please sign in to comment.