Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more Typed wrappers and make sure they're all exported #4866

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> = 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 }