Skip to content

Commit ff798fc

Browse files
authored
fix(types): useQuery types (#5803)
* fix: add extra overload for the part that's handled by the implementation * refactor: remove generics from overload implementation so that we can easily see that this is the implementation that doesn't contribute to the types interface at all * feat: export types
1 parent 39b14fa commit ff798fc

File tree

4 files changed

+33
-70
lines changed

4 files changed

+33
-70
lines changed

packages/react-query/src/__tests__/useQuery.types.test.tsx

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useQuery } from '../useQuery'
22
import { queryOptions } from '../queryOptions'
33
import { doNotExecute } from './utils'
4+
import type { UseQueryOptions } from '../types'
45
import type { Equal, Expect } from './utils'
56

67
describe('initialData', () => {
@@ -99,77 +100,24 @@ describe('initialData', () => {
99100
})
100101
})
101102

102-
describe('Query key overload', () => {
103-
it('TData should always be defined when initialData is provided', () => {
103+
describe('custom hook', () => {
104+
it('should allow custom hooks using UseQueryOptions', () => {
104105
doNotExecute(() => {
105-
const { data } = useQuery({
106-
queryKey: ['key'],
107-
queryFn: () => {
108-
return {
109-
wow: true,
110-
}
111-
},
112-
initialData: {
113-
wow: true,
114-
},
115-
})
116-
117-
const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
118-
return result
119-
})
120-
})
106+
type Data = string
121107

122-
it('TData should have undefined in the union when initialData is NOT provided', () => {
123-
doNotExecute(() => {
124-
const { data } = useQuery({
125-
queryKey: ['key'],
126-
queryFn: () => {
127-
return {
128-
wow: true,
129-
}
130-
},
131-
})
108+
const useCustomQuery = (
109+
options?: Omit<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
110+
) => {
111+
return useQuery({
112+
...options,
113+
queryKey: ['todos-key'],
114+
queryFn: () => Promise.resolve('data'),
115+
})
116+
}
132117

133-
const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
134-
true
135-
return result
136-
})
137-
})
138-
})
118+
const { data } = useCustomQuery()
139119

140-
describe('Query key and func', () => {
141-
it('TData should always be defined when initialData is provided', () => {
142-
doNotExecute(() => {
143-
const { data } = useQuery({
144-
queryKey: ['key'],
145-
queryFn: () => {
146-
return {
147-
wow: true,
148-
}
149-
},
150-
initialData: {
151-
wow: true,
152-
},
153-
})
154-
155-
const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
156-
return result
157-
})
158-
})
159-
160-
it('TData should have undefined in the union when initialData is NOT provided', () => {
161-
doNotExecute(() => {
162-
const { data } = useQuery({
163-
queryKey: ['key'],
164-
queryFn: () => {
165-
return {
166-
wow: true,
167-
}
168-
},
169-
})
170-
171-
const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
172-
true
120+
const result: Expect<Equal<Data | undefined, typeof data>> = true
173121
return result
174122
})
175123
})

packages/react-query/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ export { useQuery } from './useQuery'
1111
export { useSuspenseQuery } from './useSuspenseQuery'
1212
export { useSuspenseInfiniteQuery } from './useSuspenseInfiniteQuery'
1313
export { queryOptions } from './queryOptions'
14+
export type {
15+
DefinedInitialDataOptions,
16+
UndefinedInitialDataOptions,
17+
} from './queryOptions'
1418
export { infiniteQueryOptions } from './infiniteQueryOptions'
19+
export type {
20+
DefinedInitialDataInfiniteOptions,
21+
UndefinedInitialDataInfiniteOptions,
22+
} from './infiniteQueryOptions'
1523
export {
1624
QueryClientContext,
1725
QueryClientProvider,

packages/react-query/src/useInfiniteQuery.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,16 @@ export function useInfiniteQuery<
7070
TPageParam
7171
>,
7272
queryClient?: QueryClient,
73-
): UseInfiniteQueryResult<TData, TError> {
73+
): UseInfiniteQueryResult<TData, TError>
74+
75+
export function useInfiniteQuery(
76+
options: UseInfiniteQueryOptions,
77+
queryClient?: QueryClient,
78+
) {
7479
return useBaseQuery(
7580
options,
7681
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
7782
InfiniteQueryObserver as typeof QueryObserver,
7883
queryClient,
79-
) as UseInfiniteQueryResult<TData, TError>
84+
)
8085
}

packages/react-query/src/useQuery.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export function useQuery<
4040
>(
4141
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
4242
queryClient?: QueryClient,
43-
) {
43+
): UseQueryResult<TData, TError>
44+
45+
export function useQuery(options: UseQueryOptions, queryClient?: QueryClient) {
4446
return useBaseQuery(options, QueryObserver, queryClient)
4547
}

0 commit comments

Comments
 (0)