Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading