Skip to content

Commit

Permalink
Declare types for autogenerated infinite query hooks (#4797)
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Dec 31, 2024
1 parent 570fb22 commit 26bae54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/toolkit/src/query/react/namedHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import type {
EndpointDefinitions,
MutationDefinition,
QueryDefinition,
InfiniteQueryDefinition,
} from '@reduxjs/toolkit/query'
import type { UseLazyQuery, UseMutation, UseQuery } from './buildHooks'
import type {
UseInfiniteQuery,
UseLazyQuery,
UseMutation,
UseQuery,
} from './buildHooks'

type QueryHookNames<Definitions extends EndpointDefinitions> = {
[K in keyof Definitions as Definitions[K] extends {
Expand All @@ -26,6 +32,16 @@ type LazyQueryHookNames<Definitions extends EndpointDefinitions> = {
>
}

type InfiniteQueryHookNames<Definitions extends EndpointDefinitions> = {
[K in keyof Definitions as Definitions[K] extends {
type: DefinitionType.infinitequery
}
? `use${Capitalize<K & string>}InfiniteQuery`
: never]: UseInfiniteQuery<
Extract<Definitions[K], InfiniteQueryDefinition<any, any, any, any, any>>
>
}

type MutationHookNames<Definitions extends EndpointDefinitions> = {
[K in keyof Definitions as Definitions[K] extends {
type: DefinitionType.mutation
Expand All @@ -39,4 +55,5 @@ type MutationHookNames<Definitions extends EndpointDefinitions> = {
export type HooksWithUniqueNames<Definitions extends EndpointDefinitions> =
QueryHookNames<Definitions> &
LazyQueryHookNames<Definitions> &
InfiniteQueryHookNames<Definitions> &
MutationHookNames<Definitions>
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ describe('hooks tests', () => {
isUninitialized,
fetchNextPage,
fetchPreviousPage,
} = api.endpoints.getInfinitePokemon.useInfiniteQuery(arg, {
} = api.useGetInfinitePokemonInfiniteQuery(arg, {
initialPageParam,
})

Expand Down
2 changes: 2 additions & 0 deletions packages/toolkit/src/query/tests/infiniteQueries.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ describe('Infinite queries', () => {
.parameter(0)
.toBeString()

expectTypeOf(pokemonApi.useGetInfinitePokemonInfiniteQuery).toBeFunction()

const res = storeRef.store.dispatch(
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {}),
)
Expand Down

0 comments on commit 26bae54

Please sign in to comment.