Skip to content

Commit 1cb709b

Browse files
authored
Merge pull request #4147 from reduxjs/more-typed-wrappers
Create more Typed wrappers for RTKQ hook types
2 parents 242a016 + 02fa764 commit 1cb709b

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-1
lines changed

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export type UseQuery<D extends QueryDefinition<any, any, any, any>> = <
9898
options?: UseQuerySubscriptionOptions & UseQueryStateOptions<D, R>,
9999
) => UseQueryHookResult<D, R>
100100

101+
export type TypedUseQuery<
102+
ResultType,
103+
QueryArg,
104+
BaseQuery extends BaseQueryFn,
105+
> = UseQuery<QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>>
106+
101107
export type UseQueryHookResult<
102108
D extends QueryDefinition<any, any, any, any>,
103109
R = UseQueryStateDefaultResult<D>,
@@ -182,6 +188,14 @@ export type UseQuerySubscription<
182188
options?: UseQuerySubscriptionOptions,
183189
) => UseQuerySubscriptionResult<D>
184190

191+
export type TypedUseQuerySubscription<
192+
ResultType,
193+
QueryArg,
194+
BaseQuery extends BaseQueryFn,
195+
> = UseQuerySubscription<
196+
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
197+
>
198+
185199
export type UseQuerySubscriptionResult<
186200
D extends QueryDefinition<any, any, any, any>,
187201
> = Pick<QueryActionCreatorResult<D>, 'refetch'>
@@ -231,6 +245,14 @@ export type UseLazyQuery<D extends QueryDefinition<any, any, any, any>> = <
231245
UseLazyQueryLastPromiseInfo<D>,
232246
]
233247

248+
export type TypedUseLazyQuery<
249+
ResultType,
250+
QueryArg,
251+
BaseQuery extends BaseQueryFn,
252+
> = UseLazyQuery<
253+
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
254+
>
255+
234256
export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> = {
235257
/**
236258
* Triggers a lazy query.
@@ -258,6 +280,14 @@ export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> = {
258280
): QueryActionCreatorResult<D>
259281
}
260282

283+
export type TypedLazyQueryTrigger<
284+
ResultType,
285+
QueryArg,
286+
BaseQuery extends BaseQueryFn,
287+
> = LazyQueryTrigger<
288+
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
289+
>
290+
261291
/**
262292
* A React hook similar to [`useQuerySubscription`](#usequerysubscription), but with manual control over when the data fetching occurs.
263293
*
@@ -275,6 +305,14 @@ export type UseLazyQuerySubscription<
275305
options?: SubscriptionOptions,
276306
) => readonly [LazyQueryTrigger<D>, QueryArgFrom<D> | UninitializedValue]
277307

308+
export type TypedUseLazyQuerySubscription<
309+
ResultType,
310+
QueryArg,
311+
BaseQuery extends BaseQueryFn,
312+
> = UseLazyQuerySubscription<
313+
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
314+
>
315+
278316
export type QueryStateSelector<
279317
R extends Record<string, any>,
280318
D extends QueryDefinition<any, any, any, any>,
@@ -297,6 +335,14 @@ export type UseQueryState<D extends QueryDefinition<any, any, any, any>> = <
297335
options?: UseQueryStateOptions<D, R>,
298336
) => UseQueryStateResult<D, R>
299337

338+
export type TypedUseQueryState<
339+
ResultType,
340+
QueryArg,
341+
BaseQuery extends BaseQueryFn,
342+
> = UseQueryState<
343+
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
344+
>
345+
300346
export type UseQueryStateOptions<
301347
D extends QueryDefinition<any, any, any, any>,
302348
R extends Record<string, any>,
@@ -514,6 +560,14 @@ export type UseMutation<D extends MutationDefinition<any, any, any, any>> = <
514560
options?: UseMutationStateOptions<D, R>,
515561
) => readonly [MutationTrigger<D>, UseMutationStateResult<D, R>]
516562

563+
export type TypedUseMutation<
564+
ResultType,
565+
QueryArg,
566+
BaseQuery extends BaseQueryFn,
567+
> = UseMutation<
568+
MutationDefinition<QueryArg, BaseQuery, string, ResultType, string>
569+
>
570+
517571
export type MutationTrigger<D extends MutationDefinition<any, any, any, any>> =
518572
{
519573
/**
@@ -535,6 +589,14 @@ export type MutationTrigger<D extends MutationDefinition<any, any, any, any>> =
535589
(arg: QueryArgFrom<D>): MutationActionCreatorResult<D>
536590
}
537591

592+
export type TypedUseMutationTrigger<
593+
ResultType,
594+
QueryArg,
595+
BaseQuery extends BaseQueryFn,
596+
> = MutationTrigger<
597+
MutationDefinition<QueryArg, BaseQuery, string, ResultType, string>
598+
>
599+
538600
/**
539601
* Wrapper around `defaultQueryStateSelector` to be used in `useQuery`.
540602
* We want the initial render to already come back with

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,13 @@ export type {
1818
TypedUseQueryHookResult,
1919
TypedUseQueryStateResult,
2020
TypedUseQuerySubscriptionResult,
21+
TypedLazyQueryTrigger,
22+
TypedUseLazyQuery,
23+
TypedUseMutation,
24+
TypedUseMutationTrigger,
25+
TypedUseQueryState,
26+
TypedUseQuery,
27+
TypedUseQuerySubscription,
28+
TypedUseLazyQuerySubscription,
2129
} from './buildHooks'
2230
export { createApi, reactHooksModule, reactHooksModuleName }

packages/toolkit/src/query/tests/buildHooks.test-d.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('type tests', () => {
193193
}
194194
})
195195

196-
test('selectFromResult (query) behaviors', () => {
196+
test('top level named hooks', () => {
197197
interface Post {
198198
id: number
199199
name: string

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ import type {
33
FetchBaseQueryError,
44
TypedUseMutationResult,
55
TypedUseQueryHookResult,
6+
TypedUseQueryState,
67
TypedUseQueryStateResult,
78
TypedUseQuerySubscriptionResult,
9+
TypedLazyQueryTrigger,
10+
TypedUseLazyQuery,
11+
TypedUseLazyQuerySubscription,
12+
TypedUseMutation,
13+
TypedUseMutationTrigger,
14+
TypedUseQuerySubscription,
15+
TypedUseQuery,
816
} from '@reduxjs/toolkit/query/react'
917
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
1018

@@ -725,6 +733,10 @@ describe('union types', () => {
725733

726734
describe('"Typed" helper types', () => {
727735
test('useQuery', () => {
736+
expectTypeOf<TypedUseQuery<string, void, typeof baseQuery>>().toMatchTypeOf(
737+
api.endpoints.getTest.useQuery,
738+
)
739+
728740
const result = api.endpoints.getTest.useQuery()
729741

730742
expectTypeOf<
@@ -743,6 +755,10 @@ describe('"Typed" helper types', () => {
743755
})
744756

745757
test('useQueryState', () => {
758+
expectTypeOf<
759+
TypedUseQueryState<string, void, typeof baseQuery>
760+
>().toMatchTypeOf(api.endpoints.getTest.useQueryState)
761+
746762
const result = api.endpoints.getTest.useQueryState()
747763

748764
expectTypeOf<
@@ -761,18 +777,112 @@ describe('"Typed" helper types', () => {
761777
})
762778

763779
test('useQuerySubscription', () => {
780+
expectTypeOf<
781+
TypedUseQuerySubscription<string, void, typeof baseQuery>
782+
>().toMatchTypeOf(api.endpoints.getTest.useQuerySubscription)
783+
764784
const result = api.endpoints.getTest.useQuerySubscription()
765785

766786
expectTypeOf<
767787
TypedUseQuerySubscriptionResult<string, void, typeof baseQuery>
768788
>().toEqualTypeOf(result)
769789
})
770790

791+
test('useLazyQuery', () => {
792+
expectTypeOf<
793+
TypedUseLazyQuery<string, void, typeof baseQuery>
794+
>().toMatchTypeOf(api.endpoints.getTest.useLazyQuery)
795+
796+
const [trigger, result] = api.endpoints.getTest.useLazyQuery()
797+
798+
expectTypeOf<
799+
TypedLazyQueryTrigger<string, void, typeof baseQuery>
800+
>().toMatchTypeOf(trigger)
801+
802+
expectTypeOf<
803+
TypedUseQueryHookResult<string, void, typeof baseQuery>
804+
>().toMatchTypeOf(result)
805+
})
806+
807+
test('useLazyQuery with selectFromResult', () => {
808+
const [trigger, result] = api.endpoints.getTest.useLazyQuery({
809+
selectFromResult: () => ({ x: true }),
810+
})
811+
812+
expectTypeOf<
813+
TypedLazyQueryTrigger<string, void, typeof baseQuery>
814+
>().toMatchTypeOf(trigger)
815+
816+
expectTypeOf<
817+
TypedUseQueryHookResult<string, void, typeof baseQuery, { x: boolean }>
818+
>().toMatchTypeOf(result)
819+
})
820+
821+
test('useLazyQuerySubscription', () => {
822+
expectTypeOf<
823+
TypedUseLazyQuerySubscription<string, void, typeof baseQuery>
824+
>().toMatchTypeOf(api.endpoints.getTest.useLazyQuerySubscription)
825+
826+
const [trigger] = api.endpoints.getTest.useLazyQuerySubscription()
827+
828+
expectTypeOf<
829+
TypedLazyQueryTrigger<string, void, typeof baseQuery>
830+
>().toMatchTypeOf(trigger)
831+
})
832+
771833
test('useMutation', () => {
834+
expectTypeOf<
835+
TypedUseMutation<string, void, typeof baseQuery>
836+
>().toMatchTypeOf(api.endpoints.mutation.useMutation)
837+
772838
const [trigger, result] = api.endpoints.mutation.useMutation()
773839

840+
expectTypeOf<
841+
TypedUseMutationTrigger<string, void, typeof baseQuery>
842+
>().toMatchTypeOf(trigger)
843+
774844
expectTypeOf<
775845
TypedUseMutationResult<string, void, typeof baseQuery>
776846
>().toMatchTypeOf(result)
777847
})
848+
849+
test('useQuery - defining selectFromResult separately', () => {
850+
const selectFromResult = (
851+
result: TypedUseQueryStateResult<string, void, typeof baseQuery>,
852+
) => ({ x: true })
853+
854+
const result = api.endpoints.getTest.useQuery(undefined, {
855+
selectFromResult,
856+
})
857+
858+
expectTypeOf(result).toEqualTypeOf<
859+
TypedUseQueryHookResult<
860+
string,
861+
void,
862+
typeof baseQuery,
863+
ReturnType<typeof selectFromResult>
864+
>
865+
>()
866+
})
867+
868+
test('useMutation - defining selectFromResult separately', () => {
869+
const selectFromResult = (
870+
result: Omit<
871+
TypedUseMutationResult<string, void, typeof baseQuery>,
872+
'reset' | 'originalArgs'
873+
>,
874+
) => ({ x: true })
875+
876+
const [trigger, result] = api.endpoints.mutation.useMutation({
877+
selectFromResult,
878+
})
879+
expectTypeOf(result).toEqualTypeOf<
880+
TypedUseMutationResult<
881+
string,
882+
void,
883+
typeof baseQuery,
884+
ReturnType<typeof selectFromResult>
885+
>
886+
>()
887+
})
778888
})

0 commit comments

Comments
 (0)