Skip to content

Commit

Permalink
fix(types): add pageParam and direction as optional to "normal" query…
Browse files Browse the repository at this point in the history
…Fn (#7212)

* fix(types): add pageParam and direction as optional to "normal" queryFn

because that's what we get when using a defaultQueryFn

* chore: remove tests that are no longer true

* chore: cleanup

* wth?
  • Loading branch information
TkDodo authored Apr 2, 2024
1 parent 3cc0c15 commit e5bfbbe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
1 change: 0 additions & 1 deletion packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ describe('query', () => {
expect(queryFn).toHaveBeenCalledTimes(1)
const args = queryFn.mock.calls[0]![0]
expect(args).toBeDefined()
// @ts-expect-error page param should be undefined
expect(args.pageParam).toBeUndefined()
expect(args.queryKey).toEqual(key)
expect(args.signal).toBeInstanceOf(AbortSignal)
Expand Down
24 changes: 23 additions & 1 deletion packages/query-core/src/__tests__/queryClient.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expectTypeOf, it } from 'vitest'
import { QueryClient } from '../queryClient'
import type { DataTag, InfiniteData } from '../types'
import type { FetchDirection } from '../query'
import type { DataTag, InfiniteData, QueryKey } from '../types'

describe('getQueryData', () => {
it('should be typed if key is tagged', () => {
Expand Down Expand Up @@ -133,3 +134,24 @@ describe('fetchInfiniteQuery', () => {
})
})
})

describe('defaultOptions', () => {
it('should have a typed QueryFunctionContext', () => {
new QueryClient({
defaultOptions: {
queries: {
queryFn: (context) => {
expectTypeOf(context).toEqualTypeOf<{
queryKey: QueryKey
meta: Record<string, unknown> | undefined
signal: AbortSignal
pageParam?: unknown
direction?: FetchDirection
}>()
return Promise.resolve('data')
},
},
},
})
})
})
2 changes: 2 additions & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export type QueryFunctionContext<
queryKey: TQueryKey
signal: AbortSignal
meta: QueryMeta | undefined
pageParam?: unknown
direction?: 'forward' | 'backward'
}
: {
queryKey: TQueryKey
Expand Down
21 changes: 0 additions & 21 deletions packages/react-query/src/__tests__/useInfiniteQuery.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, expectTypeOf, it } from 'vitest'
import { QueryClient } from '@tanstack/query-core'
import { useInfiniteQuery } from '../useInfiniteQuery'
import { useQuery } from '../useQuery'
import type { InfiniteData } from '@tanstack/query-core'

describe('pageParam', () => {
Expand All @@ -27,26 +26,6 @@ describe('pageParam', () => {
})
})

it('there should be no pageParam passed to the queryFn of useQuery', () => {
useQuery({
queryKey: ['key'],
// @ts-expect-error there should be no pageParam passed to queryFn of useQuery
queryFn: ({ pageParam }) => {
return String(pageParam)
},
})
})

it('there should be no direction passed to the queryFn of useQuery', () => {
useQuery({
queryKey: ['key'],
// @ts-expect-error there should be no pageParam passed to queryFn of useQuery
queryFn: ({ direction }) => {
return String(direction)
},
})
})

it('initialPageParam should define type of param passed to queryFunctionContext for fetchInfiniteQuery', () => {
const queryClient = new QueryClient()
queryClient.fetchInfiniteQuery({
Expand Down

0 comments on commit e5bfbbe

Please sign in to comment.