Skip to content

Commit

Permalink
Fix createQuery types
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins committed Jul 20, 2024
1 parent 837bb0e commit f1fb234
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/svelte-query/src/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export function createQuery<
TQueryKey extends QueryKey = QueryKey,
>(
options: StoreOrVal<
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
queryClient?: QueryClient,
): CreateQueryResult<TData, TError>
): DefinedCreateQueryResult<TData, TError>

export function createQuery<
TQueryFnData = unknown,
Expand All @@ -31,13 +31,13 @@ export function createQuery<
TQueryKey extends QueryKey = QueryKey,
>(
options: StoreOrVal<
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
queryClient?: QueryClient,
): DefinedCreateQueryResult<TData, TError>
): CreateQueryResult<TData, TError>

export function createQuery<
TQueryFnData,
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
Expand All @@ -46,6 +46,11 @@ export function createQuery<
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
>,
queryClient?: QueryClient,
): CreateQueryResult<TData, TError>

export function createQuery(
options: StoreOrVal<CreateQueryOptions>,
queryClient?: QueryClient,
) {
return createBaseQuery(options, QueryObserver, queryClient)
}
44 changes: 44 additions & 0 deletions packages/svelte-query/tests/createQueries/createQueries.test-d.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -21,4 +24,45 @@ describe('createQueries', () => {

expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})

test('Allow custom hooks using UseQueryOptions', () => {
type Data = string

const useCustomQueries = (
options?: OmitKeyof<CreateQueryOptions<Data>, '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<Data | undefined>()
})

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<number, Error>
>()
expectTypeOf(firstResult.data).toEqualTypeOf<number | undefined>()
})
})
65 changes: 65 additions & 0 deletions packages/svelte-query/tests/createQuery/createQuery.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>,
) => {
return createQuery({
...options,
queryKey: ['todos-key'],
queryFn: () => Promise.resolve('data'),
})
}

const query = useCustomQuery()

expectTypeOf(get(query).data).toEqualTypeOf<Data | undefined>()
})
})

0 comments on commit f1fb234

Please sign in to comment.