diff --git a/packages/svelte-query/src/createQuery.ts b/packages/svelte-query/src/createQuery.ts index 08417d54c2..32bf45e394 100644 --- a/packages/svelte-query/src/createQuery.ts +++ b/packages/svelte-query/src/createQuery.ts @@ -19,10 +19,10 @@ export function createQuery< TQueryKey extends QueryKey = QueryKey, >( options: StoreOrVal< - UndefinedInitialDataOptions + DefinedInitialDataOptions >, queryClient?: QueryClient, -): CreateQueryResult +): DefinedCreateQueryResult export function createQuery< TQueryFnData = unknown, @@ -31,13 +31,13 @@ export function createQuery< TQueryKey extends QueryKey = QueryKey, >( options: StoreOrVal< - DefinedInitialDataOptions + UndefinedInitialDataOptions >, queryClient?: QueryClient, -): DefinedCreateQueryResult +): CreateQueryResult export function createQuery< - TQueryFnData, + TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, @@ -46,6 +46,11 @@ export function createQuery< CreateQueryOptions >, queryClient?: QueryClient, +): CreateQueryResult + +export function createQuery( + options: StoreOrVal, + queryClient?: QueryClient, ) { return createBaseQuery(options, QueryObserver, queryClient) } diff --git a/packages/svelte-query/tests/createQueries/createQueries.test-d.ts b/packages/svelte-query/tests/createQueries/createQueries.test-d.ts index 32721b2f49..0f3881330a 100644 --- a/packages/svelte-query/tests/createQueries/createQueries.test-d.ts +++ b/packages/svelte-query/tests/createQueries/createQueries.test-d.ts @@ -1,6 +1,9 @@ import { describe, expectTypeOf, test } from 'vitest' import { get } from 'svelte/store' +import { skipToken } from '@tanstack/query-core' import { createQueries, queryOptions } from '../../src/index' +import type { OmitKeyof, QueryObserverResult } from '@tanstack/query-core' +import type { CreateQueryOptions } from '../../src/index' describe('createQueries', () => { test('TData should be defined when passed through queryOptions', () => { @@ -21,4 +24,45 @@ describe('createQueries', () => { expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>() }) + + test('Allow custom hooks using UseQueryOptions', () => { + type Data = string + + const useCustomQueries = ( + options?: OmitKeyof, 'queryKey' | 'queryFn'>, + ) => { + return createQueries({ + queries: [ + { + ...options, + queryKey: ['todos-key'], + queryFn: () => Promise.resolve('data'), + }, + ], + }) + } + + const query = useCustomQueries() + const data = get(query)[0].data + + expectTypeOf(data).toEqualTypeOf() + }) + + test('TData should have correct type when conditional skipToken is passed', () => { + const queryResults = createQueries({ + queries: [ + { + queryKey: ['withSkipToken'], + queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5), + }, + ], + }) + + const firstResult = get(queryResults)[0] + + expectTypeOf(firstResult).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(firstResult.data).toEqualTypeOf() + }) }) diff --git a/packages/svelte-query/tests/createQuery/createQuery.test-d.ts b/packages/svelte-query/tests/createQuery/createQuery.test-d.ts new file mode 100644 index 0000000000..f2931340e9 --- /dev/null +++ b/packages/svelte-query/tests/createQuery/createQuery.test-d.ts @@ -0,0 +1,65 @@ +import { describe, expectTypeOf, test } from 'vitest' +import { get } from 'svelte/store' +import { createQuery, queryOptions } from '../../src/index' +import type { OmitKeyof } from '@tanstack/query-core' +import type { CreateQueryOptions } from '../../src/index' + +describe('createQuery', () => { + test('TData should always be defined when initialData is provided as an object', () => { + const query = createQuery({ + queryKey: ['key'], + queryFn: () => ({ wow: true }), + initialData: { wow: true }, + }) + + expectTypeOf(get(query).data).toEqualTypeOf<{ wow: boolean }>() + }) + + test('TData should be defined when passed through queryOptions', () => { + const options = queryOptions({ + queryKey: ['key'], + queryFn: () => { + return { + wow: true, + } + }, + initialData: { + wow: true, + }, + }) + const query = createQuery(options) + + expectTypeOf(get(query).data).toEqualTypeOf<{ wow: boolean }>() + }) + + test('TData should have undefined in the union when initialData is NOT provided', () => { + const query = createQuery({ + queryKey: ['key'], + queryFn: () => { + return { + wow: true, + } + }, + }) + + expectTypeOf(get(query).data).toEqualTypeOf<{ wow: boolean } | undefined>() + }) + + test('Allow custom hooks using CreateQueryOptions', () => { + type Data = string + + const useCustomQuery = ( + options?: OmitKeyof, 'queryKey' | 'queryFn'>, + ) => { + return createQuery({ + ...options, + queryKey: ['todos-key'], + queryFn: () => Promise.resolve('data'), + }) + } + + const query = useCustomQuery() + + expectTypeOf(get(query).data).toEqualTypeOf() + }) +})