Skip to content

Commit 80c9568

Browse files
committed
Fix infinite query hook data types
1 parent effdb7d commit 80c9568

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import type { InfiniteQueryActionCreatorResult } from '@internal/query/core/buil
5757
import type {
5858
InfiniteData,
5959
InfiniteQueryConfigOptions,
60+
InfiniteQuerySubState,
6061
} from '@internal/query/core/apiState'
6162
import type { InfiniteQueryResultSelectorResult } from '../core/buildSelectors'
6263
import type {
@@ -980,13 +981,13 @@ export type UseInfiniteQueryStateResult<
980981

981982
type UseInfiniteQueryStateBaseResult<
982983
D extends InfiniteQueryDefinition<any, any, any, any, any>,
983-
> = QuerySubState<D> & {
984+
> = InfiniteQuerySubState<D> & {
984985
/**
985986
* Where `data` tries to hold data as much as possible, also re-using
986987
* data from the last arguments passed into the hook, this property
987988
* will always contain the received data from the query, for the current query arguments.
988989
*/
989-
currentData?: ResultTypeFrom<D>
990+
currentData?: InfiniteData<ResultTypeFrom<D>, PageParamFrom<D>>
990991
/**
991992
* Query has not started yet.
992993
*/

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,15 @@ describe('hooks tests', () => {
778778
}),
779779
)
780780

781+
type Pokemon = {
782+
id: string
783+
name: string
784+
}
785+
781786
const pokemonApi = createApi({
782787
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
783788
endpoints: (builder) => ({
784-
getInfinitePokemon: builder.infiniteQuery<any, string, number>({
789+
getInfinitePokemon: builder.infiniteQuery<Pokemon[], string, number>({
785790
infiniteQueryOptions: {
786791
initialPageParam: 0,
787792
getNextPageParam: (
@@ -815,7 +820,13 @@ describe('hooks tests', () => {
815820
const { data, isFetching, isUninitialized, fetchNextPage } =
816821
pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuery('a', {
817822
initialPageParam: 0,
818-
getNextPageParam: (lastPageParam) => lastPageParam + 1,
823+
getNextPageParam: (
824+
lastPage,
825+
allPages,
826+
// Page param type should be `number`
827+
lastPageParam,
828+
allPageParams,
829+
) => lastPageParam + 1,
819830
})
820831

821832
return (

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { skipToken, InfiniteData } from '@reduxjs/toolkit/query/react'
12
import {
23
createApi,
34
fetchBaseQuery,
@@ -68,5 +69,37 @@ describe('Infinite queries', () => {
6869
direction: 'forward',
6970
}),
7071
)
72+
73+
const useGetInfinitePokemonQuery =
74+
pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuery
75+
76+
expectTypeOf(useGetInfinitePokemonQuery)
77+
.parameter(0)
78+
.toEqualTypeOf<string | typeof skipToken>()
79+
80+
function PokemonList() {
81+
const {
82+
data,
83+
currentData,
84+
isFetching,
85+
isUninitialized,
86+
isSuccess,
87+
fetchNextPage,
88+
} = useGetInfinitePokemonQuery('a')
89+
90+
expectTypeOf(data).toEqualTypeOf<
91+
InfiniteData<Pokemon[], number> | undefined
92+
>()
93+
94+
if (isSuccess) {
95+
expectTypeOf(data.pages).toEqualTypeOf<Pokemon[][]>()
96+
expectTypeOf(data.pageParams).toEqualTypeOf<number[]>()
97+
}
98+
99+
if (currentData) {
100+
expectTypeOf(currentData.pages).toEqualTypeOf<Pokemon[][]>()
101+
expectTypeOf(currentData.pageParams).toEqualTypeOf<number[]>()
102+
}
103+
}
71104
})
72105
})

0 commit comments

Comments
 (0)