From 26bae549decad9836bbd3d5563830d82f281b346 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Tue, 31 Dec 2024 16:28:46 -0500 Subject: [PATCH] Declare types for autogenerated infinite query hooks (#4797) --- .../toolkit/src/query/react/namedHooks.ts | 19 ++++++++++++++++++- .../src/query/tests/buildHooks.test.tsx | 2 +- .../src/query/tests/infiniteQueries.test-d.ts | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/toolkit/src/query/react/namedHooks.ts b/packages/toolkit/src/query/react/namedHooks.ts index 07b7ac7417..ba669dda93 100644 --- a/packages/toolkit/src/query/react/namedHooks.ts +++ b/packages/toolkit/src/query/react/namedHooks.ts @@ -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 = { [K in keyof Definitions as Definitions[K] extends { @@ -26,6 +32,16 @@ type LazyQueryHookNames = { > } +type InfiniteQueryHookNames = { + [K in keyof Definitions as Definitions[K] extends { + type: DefinitionType.infinitequery + } + ? `use${Capitalize}InfiniteQuery` + : never]: UseInfiniteQuery< + Extract> + > +} + type MutationHookNames = { [K in keyof Definitions as Definitions[K] extends { type: DefinitionType.mutation @@ -39,4 +55,5 @@ type MutationHookNames = { export type HooksWithUniqueNames = QueryHookNames & LazyQueryHookNames & + InfiniteQueryHookNames & MutationHookNames diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx index 95e84213ce..13ffb48b20 100644 --- a/packages/toolkit/src/query/tests/buildHooks.test.tsx +++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx @@ -1764,7 +1764,7 @@ describe('hooks tests', () => { isUninitialized, fetchNextPage, fetchPreviousPage, - } = api.endpoints.getInfinitePokemon.useInfiniteQuery(arg, { + } = api.useGetInfinitePokemonInfiniteQuery(arg, { initialPageParam, }) diff --git a/packages/toolkit/src/query/tests/infiniteQueries.test-d.ts b/packages/toolkit/src/query/tests/infiniteQueries.test-d.ts index 67b52ac7bb..1657534339 100644 --- a/packages/toolkit/src/query/tests/infiniteQueries.test-d.ts +++ b/packages/toolkit/src/query/tests/infiniteQueries.test-d.ts @@ -53,6 +53,8 @@ describe('Infinite queries', () => { .parameter(0) .toBeString() + expectTypeOf(pokemonApi.useGetInfinitePokemonInfiniteQuery).toBeFunction() + const res = storeRef.store.dispatch( pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {}), )