From bd212a5b60b07b11d9eb569f43c313710b473254 Mon Sep 17 00:00:00 2001 From: chosunghoon Date: Wed, 9 Oct 2024 21:34:38 +0900 Subject: [PATCH 1/3] fix(react-query): Allow optional initialData in infiniteQueryoptions --- packages/react-query/src/infiniteQueryOptions.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-query/src/infiniteQueryOptions.ts b/packages/react-query/src/infiniteQueryOptions.ts index a63def60e1..0137869745 100644 --- a/packages/react-query/src/infiniteQueryOptions.ts +++ b/packages/react-query/src/infiniteQueryOptions.ts @@ -42,6 +42,7 @@ export type DefinedInitialDataInfiniteOptions< initialData: | NonUndefinedGuard> | (() => NonUndefinedGuard>) + | undefined } export function infiniteQueryOptions< From 44dc4a5992876590a0d5e5f7184be1bf0174674a Mon Sep 17 00:00:00 2001 From: sungpaks Date: Thu, 10 Oct 2024 20:56:29 +0900 Subject: [PATCH 2/3] fix(react-query): Allow optional initialData object in queryOptions --- .../src/__tests__/queryOptions.test-d.tsx | 20 ++++++++++++++++++- packages/react-query/src/queryOptions.ts | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/react-query/src/__tests__/queryOptions.test-d.tsx b/packages/react-query/src/__tests__/queryOptions.test-d.tsx index 03958a63a0..f833d0db5a 100644 --- a/packages/react-query/src/__tests__/queryOptions.test-d.tsx +++ b/packages/react-query/src/__tests__/queryOptions.test-d.tsx @@ -9,7 +9,10 @@ import { queryOptions } from '../queryOptions' import { useQuery } from '../useQuery' import { useQueries } from '../useQueries' import { useSuspenseQuery } from '../useSuspenseQuery' -import type { QueryObserverResult } from '@tanstack/query-core' +import type { + InitialDataFunction, + QueryObserverResult, +} from '@tanstack/query-core' describe('queryOptions', () => { it('should not allow excess properties', () => { @@ -205,4 +208,19 @@ describe('queryOptions', () => { }, }) }) + + it('should allow optional initialData object', () => { + const testFn = (id?: string) => { + const options = queryOptions({ + queryKey: ['test'], + queryFn: async () => 'something string', + initialData: id ? 'initial string' : undefined, + }) + expectTypeOf(options.initialData).toMatchTypeOf< + InitialDataFunction | string | undefined + >() + } + testFn('id') + testFn() + }) }) diff --git a/packages/react-query/src/queryOptions.ts b/packages/react-query/src/queryOptions.ts index ec1c11aa2c..b659cc034f 100644 --- a/packages/react-query/src/queryOptions.ts +++ b/packages/react-query/src/queryOptions.ts @@ -12,7 +12,10 @@ export type UndefinedInitialDataOptions< TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, > = UseQueryOptions & { - initialData?: undefined | InitialDataFunction> + initialData?: + | undefined + | InitialDataFunction> + | NonUndefinedGuard } type NonUndefinedGuard = T extends undefined ? never : T From 3800783ca01667366aa35123bc83d52fd6b24a03 Mon Sep 17 00:00:00 2001 From: sungpaks Date: Thu, 10 Oct 2024 20:57:34 +0900 Subject: [PATCH 3/3] refactor(react-query): removed unnecessary lines in infiniteQueryOptions test --- .../react-query/src/__tests__/infiniteQueryOptions.test-d.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx b/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx index 78243a4f3f..fd8bd57917 100644 --- a/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx +++ b/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx @@ -158,14 +158,12 @@ describe('infiniteQueryOptions', () => { 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> @@ -178,14 +176,12 @@ describe('infiniteQueryOptions', () => { 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>