Skip to content

Commit 09763dd

Browse files
committed
feat(types): make sure we can't pass infiniteQueryOptions to non-infinite query functions like fetchQuery
1 parent 8f9a460 commit 09763dd

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

packages/query-core/src/queryClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export class QueryClient {
386386
TData,
387387
TPageParam
388388
>(options.pages)
389-
return this.fetchQuery(options)
389+
return this.fetchQuery(options as any)
390390
}
391391

392392
prefetchInfiniteQuery<
@@ -429,7 +429,7 @@ export class QueryClient {
429429
TPageParam
430430
>(options.pages)
431431

432-
return this.ensureQueryData(options)
432+
return this.ensureQueryData(options as any)
433433
}
434434

435435
resumePausedMutations(): Promise<unknown> {

packages/query-core/src/types.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ export interface FetchQueryOptions<
448448
QueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>,
449449
'queryKey'
450450
> {
451+
initialPageParam?: never
451452
/**
452453
* The time in milliseconds after data is considered stale.
453454
* If the data is fresh it will be returned from the cache.
@@ -500,12 +501,15 @@ export type FetchInfiniteQueryOptions<
500501
TData = TQueryFnData,
501502
TQueryKey extends QueryKey = QueryKey,
502503
TPageParam = unknown,
503-
> = FetchQueryOptions<
504-
TQueryFnData,
505-
TError,
506-
InfiniteData<TData, TPageParam>,
507-
TQueryKey,
508-
TPageParam
504+
> = Omit<
505+
FetchQueryOptions<
506+
TQueryFnData,
507+
TError,
508+
InfiniteData<TData, TPageParam>,
509+
TQueryKey,
510+
TPageParam
511+
>,
512+
'initialPageParam'
509513
> &
510514
InitialPageParam<TPageParam> &
511515
FetchInfiniteQueryPages<TQueryFnData, TPageParam>

packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { describe, expectTypeOf, it } from 'vitest'
1+
import { describe, expectTypeOf, it, test } from 'vitest'
22
import { QueryClient, dataTagSymbol } from '@tanstack/query-core'
33
import { infiniteQueryOptions } from '../infiniteQueryOptions'
44
import { useInfiniteQuery } from '../useInfiniteQuery'
55
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
6+
import { useQuery } from '../useQuery'
67
import type { InfiniteData } from '@tanstack/query-core'
78

89
describe('queryOptions', () => {
@@ -133,4 +134,22 @@ describe('queryOptions', () => {
133134
InfiniteData<string, unknown> | undefined
134135
>()
135136
})
137+
138+
test('should not be allowed to be passed to non-infinite query functions', () => {
139+
const queryClient = new QueryClient()
140+
const options = infiniteQueryOptions({
141+
queryKey: ['key'],
142+
queryFn: () => Promise.resolve('string'),
143+
getNextPageParam: () => 1,
144+
initialPageParam: 1,
145+
})
146+
// @ts-expect-error cannot pass infinite options to non-infinite query functions
147+
useQuery(options)
148+
// @ts-expect-error cannot pass infinite options to non-infinite query functions
149+
queryClient.ensureQueryData(options)
150+
// @ts-expect-error cannot pass infinite options to non-infinite query functions
151+
queryClient.fetchQuery(options)
152+
// @ts-expect-error cannot pass infinite options to non-infinite query functions
153+
queryClient.prefetchQuery(options)
154+
})
136155
})

0 commit comments

Comments
 (0)