diff --git a/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts b/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts index 4afe19c2af..5ec64e83bb 100644 --- a/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts +++ b/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts @@ -25,6 +25,25 @@ describe('queryOptions', () => { }, }) }) + + test('should allow undefined response in initialData', () => { + return (id: string | null) => + queryOptions({ + queryKey: ['todo', id], + queryFn: () => + Promise.resolve({ + id: '1', + title: 'Do Laundry', + }), + initialData: () => + !id + ? undefined + : { + id, + title: 'Initial Data', + }, + }) + }) }) test('should work when passed to injectQuery', () => { diff --git a/packages/angular-query-experimental/src/query-options.ts b/packages/angular-query-experimental/src/query-options.ts index 7e958f3df7..519ff4b20a 100644 --- a/packages/angular-query-experimental/src/query-options.ts +++ b/packages/angular-query-experimental/src/query-options.ts @@ -1,4 +1,9 @@ -import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core' +import { + DataTag, + DefaultError, + QueryKey, + InitialDataFunction, +} from '@tanstack/query-core' import type { CreateQueryOptions } from './types' export type UndefinedInitialDataOptions< @@ -20,7 +25,7 @@ export type DefinedInitialDataOptions< > = CreateQueryOptions & { initialData: | NonUndefinedGuard - | (() => NonUndefinedGuard) + | InitialDataFunction> } export function queryOptions<