From ef9bd44eb66cf53817afff14779df351ff4d2978 Mon Sep 17 00:00:00 2001 From: Jimmy Callin Date: Wed, 9 Oct 2024 17:46:47 +0200 Subject: [PATCH] allow optional initialData object --- .../__tests__/infiniteQueryOptions.test-d.tsx | 23 ++++++++++++++++++- .../react-query/src/infiniteQueryOptions.ts | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx b/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx index ceedfcaac3..0e621a8028 100644 --- a/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx +++ b/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx @@ -153,7 +153,7 @@ describe('queryOptions', () => { queryClient.prefetchQuery(options) }) - test('allow optional initialData', () => { + test('allow optional initialData function', () => { const initialData: { example: boolean } | undefined = { example: true } const queryOptions = infiniteQueryOptions({ queryKey: ['example'], @@ -168,6 +168,27 @@ describe('queryOptions', () => { queryOptions.initialData expectTypeOf(queryOptions.initialData).toMatchTypeOf< | InitialDataFunction> + | InfiniteData<{ example: boolean }, number> + | undefined + >() + }) + + test('allow optional initialData object', () => { + const initialData: { example: boolean } | undefined = { example: true } + const queryOptions = infiniteQueryOptions({ + queryKey: ['example'], + queryFn: async () => initialData, + // initialData below errors + initialData: initialData + ? { pages: [initialData], pageParams: [] } + : undefined, + getNextPageParam: () => 1, + initialPageParam: 1, + }) + queryOptions.initialData + expectTypeOf(queryOptions.initialData).toMatchTypeOf< + | InitialDataFunction> + | InfiniteData<{ example: boolean }, number> | undefined >() }) diff --git a/packages/react-query/src/infiniteQueryOptions.ts b/packages/react-query/src/infiniteQueryOptions.ts index a4273230cc..240aada9f2 100644 --- a/packages/react-query/src/infiniteQueryOptions.ts +++ b/packages/react-query/src/infiniteQueryOptions.ts @@ -23,6 +23,7 @@ export type UndefinedInitialDataInfiniteOptions< > & { initialData?: | undefined + | NonUndefinedGuard> | InitialDataFunction< NonUndefinedGuard> >