Skip to content

Commit c8f3739

Browse files
authored
Merge pull request #4604 from aryaemami59/export-UseQueryStateOptions
Add the `TypedUseQueryStateOptions` helper type
2 parents 719dac5 + e569971 commit c8f3739

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

packages/toolkit/src/query/core/apiState.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export function getRequestStatusFlags(status: QueryStatus): RequestStatusFlags {
7878
} as any
7979
}
8080

81+
/**
82+
* @public
83+
*/
8184
export type SubscriptionOptions = {
8285
/**
8386
* How frequently to automatically re-fetch data (in milliseconds). Defaults to `0` (off).

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ export type TypedUseQueryState<
357357
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
358358
>
359359

360+
/**
361+
* @internal
362+
*/
360363
export type UseQueryStateOptions<
361364
D extends QueryDefinition<any, any, any, any>,
362365
R extends Record<string, any>,
@@ -427,6 +430,79 @@ export type UseQueryStateOptions<
427430
selectFromResult?: QueryStateSelector<R, D>
428431
}
429432

433+
/**
434+
* Provides a way to define a "pre-typed" version of
435+
* {@linkcode UseQueryStateOptions} with specific options for a given query.
436+
* This is particularly useful for setting default query behaviors such as
437+
* refetching strategies, which can be overridden as needed.
438+
*
439+
* @example
440+
* <caption>#### __Create a `useQuery` hook with default options__</caption>
441+
*
442+
* ```ts
443+
* import type {
444+
* SubscriptionOptions,
445+
* TypedUseQueryStateOptions,
446+
* } from '@reduxjs/toolkit/query/react'
447+
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
448+
*
449+
* type Post = {
450+
* id: number
451+
* name: string
452+
* }
453+
*
454+
* const api = createApi({
455+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
456+
* tagTypes: ['Post'],
457+
* endpoints: (build) => ({
458+
* getPosts: build.query<Post[], void>({
459+
* query: () => 'posts',
460+
* }),
461+
* }),
462+
* })
463+
*
464+
* const { useGetPostsQuery } = api
465+
*
466+
* export const useGetPostsQueryWithDefaults = <
467+
* SelectedResult extends Record<string, any>,
468+
* >(
469+
* overrideOptions: TypedUseQueryStateOptions<
470+
* Post[],
471+
* void,
472+
* ReturnType<typeof fetchBaseQuery>,
473+
* SelectedResult
474+
* > &
475+
* SubscriptionOptions,
476+
* ) =>
477+
* useGetPostsQuery(undefined, {
478+
* // Insert default options here
479+
*
480+
* refetchOnMountOrArgChange: true,
481+
* refetchOnFocus: true,
482+
* ...overrideOptions,
483+
* })
484+
* ```
485+
*
486+
* @template ResultType - The type of the result `data` returned by the query.
487+
* @template QueryArg - The type of the argument passed into the query.
488+
* @template BaseQuery - The type of the base query function being used.
489+
* @template SelectedResult - The type of the selected result returned by the __`selectFromResult`__ function.
490+
*
491+
* @since 2.7.8
492+
* @public
493+
*/
494+
export type TypedUseQueryStateOptions<
495+
ResultType,
496+
QueryArg,
497+
BaseQuery extends BaseQueryFn,
498+
SelectedResult extends Record<string, any> = UseQueryStateDefaultResult<
499+
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
500+
>,
501+
> = UseQueryStateOptions<
502+
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>,
503+
SelectedResult
504+
>
505+
430506
export type UseQueryStateResult<
431507
_ extends QueryDefinition<any, any, any, any>,
432508
R,

packages/toolkit/src/query/react/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type {
2626
TypedUseQuery,
2727
TypedUseQuerySubscription,
2828
TypedUseLazyQuerySubscription,
29+
TypedUseQueryStateOptions,
2930
} from './buildHooks'
3031
export { UNINITIALIZED_VALUE } from './constants'
3132
export { createApi, reactHooksModule, reactHooksModuleName }

packages/toolkit/src/query/tests/unionTypes.test-d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import type { UseQueryStateOptions } from '@internal/query/react/buildHooks'
12
import type { SerializedError } from '@reduxjs/toolkit'
23
import type {
34
FetchBaseQueryError,
5+
QueryDefinition,
46
TypedUseMutationResult,
57
TypedUseQueryHookResult,
68
TypedUseQueryState,
@@ -13,6 +15,7 @@ import type {
1315
TypedMutationTrigger,
1416
TypedUseQuerySubscription,
1517
TypedUseQuery,
18+
TypedUseQueryStateOptions,
1619
} from '@reduxjs/toolkit/query/react'
1720
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
1821

@@ -776,6 +779,23 @@ describe('"Typed" helper types', () => {
776779
>().toEqualTypeOf(result)
777780
})
778781

782+
test('useQueryState options', () => {
783+
expectTypeOf<
784+
TypedUseQueryStateOptions<string, void, typeof baseQuery>
785+
>().toMatchTypeOf<
786+
Parameters<typeof api.endpoints.getTest.useQueryState>[1]
787+
>()
788+
789+
expectTypeOf<
790+
UseQueryStateOptions<
791+
QueryDefinition<void, typeof baseQuery, string, string>,
792+
{ x: boolean }
793+
>
794+
>().toEqualTypeOf<
795+
TypedUseQueryStateOptions<string, void, typeof baseQuery, { x: boolean }>
796+
>()
797+
})
798+
779799
test('useQuerySubscription', () => {
780800
expectTypeOf<
781801
TypedUseQuerySubscription<string, void, typeof baseQuery>

0 commit comments

Comments
 (0)