diff --git a/.changeset/svelte-queryoptions-optional-initialdata-object.md b/.changeset/svelte-queryoptions-optional-initialdata-object.md
new file mode 100644
index 00000000000..e3d5174cbae
--- /dev/null
+++ b/.changeset/svelte-queryoptions-optional-initialdata-object.md
@@ -0,0 +1,5 @@
+---
+'@tanstack/svelte-query': patch
+---
+
+fix(svelte-query/{createQuery,queryOptions}): correct the overload selected for 'initialData'
diff --git a/packages/svelte-query/src/createQuery.ts b/packages/svelte-query/src/createQuery.ts
index 1397334f177..a96c75f3c88 100644
--- a/packages/svelte-query/src/createQuery.ts
+++ b/packages/svelte-query/src/createQuery.ts
@@ -12,6 +12,58 @@ import type {
UndefinedInitialDataOptions,
} from './queryOptions.js'
+/**
+ * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
+ * The query runs when the options call for it — `enabled: false` skips the initial fetch.
+ *
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
+ * a `select` changes `TData` to include `undefined`).
+ *
+ * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
+ * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
+ * with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
+ * be used.
+ * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
+ * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
+ * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
+ *
+ * @example
+ * ```svelte
+ *
+ *
+ * {#if query.isError}
+ * Error: {query.error.message}
+ * {/if}
+ *
+ * {#each query.data as post (post.id)}
+ * - {post.title}
+ * {/each}
+ *
+ * ```
+ */
+export function createQuery<
+ TQueryFnData = unknown,
+ TError = DefaultError,
+ TData = TQueryFnData,
+ TQueryKey extends QueryKey = QueryKey,
+>(
+ options: Accessor<
+ DefinedInitialDataOptions
+ >,
+ queryClient?: Accessor,
+): DefinedCreateQueryResult
+
/**
* Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
* The query runs when the options call for it — `enabled: false` skips the initial fetch.
@@ -86,58 +138,6 @@ export function createQuery<
queryClient?: Accessor,
): CreateQueryResult
-/**
- * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
- * The query runs when the options call for it — `enabled: false` skips the initial fetch.
- *
- * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
- * a `select` changes `TData` to include `undefined`).
- *
- * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
- * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
- * with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
- * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
- * be used.
- * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
- * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
- * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
- *
- * @example
- * ```svelte
- *
- *
- * {#if query.isError}
- * Error: {query.error.message}
- * {/if}
- *
- * {#each query.data as post (post.id)}
- * - {post.title}
- * {/each}
- *
- * ```
- */
-export function createQuery<
- TQueryFnData = unknown,
- TError = DefaultError,
- TData = TQueryFnData,
- TQueryKey extends QueryKey = QueryKey,
->(
- options: Accessor<
- DefinedInitialDataOptions
- >,
- queryClient?: Accessor,
-): DefinedCreateQueryResult
-
/**
* @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
* @param options - The {@link CreateQueryOptions} to use — everything you can pass to `createQuery`, wrapped
diff --git a/packages/svelte-query/src/queryOptions.ts b/packages/svelte-query/src/queryOptions.ts
index f7f04f0f332..9fb3c28b234 100644
--- a/packages/svelte-query/src/queryOptions.ts
+++ b/packages/svelte-query/src/queryOptions.ts
@@ -13,7 +13,10 @@ export type UndefinedInitialDataOptions<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = CreateQueryOptions & {
- initialData?: undefined | InitialDataFunction>
+ initialData?:
+ | undefined
+ | InitialDataFunction>
+ | NonUndefinedGuard
}
export type DefinedInitialDataOptions<
diff --git a/packages/svelte-query/tests/createQuery/createQuery.test-d.ts b/packages/svelte-query/tests/createQuery/createQuery.test-d.ts
index e4f368dda00..b3cb3d15107 100644
--- a/packages/svelte-query/tests/createQuery/createQuery.test-d.ts
+++ b/packages/svelte-query/tests/createQuery/createQuery.test-d.ts
@@ -47,6 +47,17 @@ describe('createQuery', () => {
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})
+ it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
+ const key = queryKey()
+ const { data } = createQuery(() => ({
+ queryKey: key,
+ queryFn: () => ({ wow: true }),
+ initialData: () => ({ wow: true }),
+ }))
+
+ expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
+ })
+
it('TData should have undefined in the union when initialData is NOT provided', () => {
const key = queryKey()
const { data } = createQuery(() => ({
diff --git a/packages/svelte-query/tests/queryOptions.test-d.ts b/packages/svelte-query/tests/queryOptions.test-d.ts
index f3bcedbc13f..9201eb5097d 100644
--- a/packages/svelte-query/tests/queryOptions.test-d.ts
+++ b/packages/svelte-query/tests/queryOptions.test-d.ts
@@ -2,6 +2,7 @@ import { assertType, describe, expectTypeOf, it } from 'vitest'
import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
import { queryKey } from '@tanstack/query-test-utils'
import { createQueries, queryOptions } from '../src/index.js'
+import type { InitialDataFunction } from '@tanstack/query-core'
// Regression test for exported queryOptions inference under declaration emit.
// TypeScript should be able to name the return type without expanding the
@@ -247,4 +248,16 @@ describe('queryOptions', () => {
{ id: string; title: string } | undefined
>()
})
+
+ it('should allow optional initialData object', () => {
+ const options = queryOptions({
+ queryKey: queryKey(),
+ queryFn: () => Promise.resolve('something string'),
+ initialData: Math.random() > 0.5 ? 'initial string' : undefined,
+ })
+
+ expectTypeOf(options.initialData).toExtend<
+ InitialDataFunction | string | undefined
+ >()
+ })
})