Skip to content

Commit

Permalink
feat(types): make sure we can't pass infiniteQueryOptions to non-infi…
Browse files Browse the repository at this point in the history
…nite query functions like fetchQuery
  • Loading branch information
TkDodo committed Sep 12, 2024
1 parent 8f9a460 commit 09763dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class QueryClient {
TData,
TPageParam
>(options.pages)
return this.fetchQuery(options)
return this.fetchQuery(options as any)
}

prefetchInfiniteQuery<
Expand Down Expand Up @@ -429,7 +429,7 @@ export class QueryClient {
TPageParam
>(options.pages)

return this.ensureQueryData(options)
return this.ensureQueryData(options as any)
}

resumePausedMutations(): Promise<unknown> {
Expand Down
16 changes: 10 additions & 6 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export interface FetchQueryOptions<
QueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>,
'queryKey'
> {
initialPageParam?: never
/**
* The time in milliseconds after data is considered stale.
* If the data is fresh it will be returned from the cache.
Expand Down Expand Up @@ -500,12 +501,15 @@ export type FetchInfiniteQueryOptions<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> = FetchQueryOptions<
TQueryFnData,
TError,
InfiniteData<TData, TPageParam>,
TQueryKey,
TPageParam
> = Omit<
FetchQueryOptions<
TQueryFnData,
TError,
InfiniteData<TData, TPageParam>,
TQueryKey,
TPageParam
>,
'initialPageParam'
> &
InitialPageParam<TPageParam> &
FetchInfiniteQueryPages<TQueryFnData, TPageParam>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, expectTypeOf, it } from 'vitest'
import { describe, expectTypeOf, it, test } from 'vitest'
import { QueryClient, dataTagSymbol } from '@tanstack/query-core'
import { infiniteQueryOptions } from '../infiniteQueryOptions'
import { useInfiniteQuery } from '../useInfiniteQuery'
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
import { useQuery } from '../useQuery'
import type { InfiniteData } from '@tanstack/query-core'

describe('queryOptions', () => {
Expand Down Expand Up @@ -133,4 +134,22 @@ describe('queryOptions', () => {
InfiniteData<string, unknown> | undefined
>()
})

test('should not be allowed to be passed to non-infinite query functions', () => {
const queryClient = new QueryClient()
const options = infiniteQueryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})
// @ts-expect-error cannot pass infinite options to non-infinite query functions
useQuery(options)
// @ts-expect-error cannot pass infinite options to non-infinite query functions
queryClient.ensureQueryData(options)
// @ts-expect-error cannot pass infinite options to non-infinite query functions
queryClient.fetchQuery(options)
// @ts-expect-error cannot pass infinite options to non-infinite query functions
queryClient.prefetchQuery(options)
})
})

0 comments on commit 09763dd

Please sign in to comment.