@@ -18,6 +18,7 @@ import {
1818 QueryCache ,
1919 QueryFunction ,
2020 QueryFunctionContext ,
21+ UseQueryOptions ,
2122} from '../..'
2223import { ErrorBoundary } from 'react-error-boundary'
2324
@@ -36,22 +37,6 @@ describe('useQuery', () => {
3637 expectType < unknown > ( noQueryFn . data )
3738 expectType < unknown > ( noQueryFn . error )
3839
39- // it should not be possible for queryFn to return undefined
40- // @ts -expect-error (queryFn returns undefined)
41- useQuery ( key , ( ) => undefined )
42-
43- // it should not be possible for queryFn to have explicit void return type
44- // @ts -expect-error (queryFn explicit return type is void)
45- useQuery ( key , ( ) : void => undefined )
46-
47- // it should not be possible for queryFn to have explicit Promise<void> return type
48- // @ts -expect-error (queryFn explicit return type is Promise<void>)
49- useQuery ( key , ( ) : Promise < void > => Promise . resolve ( ) )
50-
51- // it should not be possible for queryFn to have explicit Promise<undefined> return type
52- // @ts -expect-error (queryFn explicit return type is Promise<undefined>)
53- useQuery ( key , ( ) : Promise < undefined > => Promise . resolve ( undefined ) )
54-
5540 // it should infer the result type from the query function
5641 const fromQueryFn = useQuery ( key , ( ) => 'test' )
5742 expectType < string | undefined > ( fromQueryFn . data )
@@ -137,6 +122,49 @@ describe('useQuery', () => {
137122 queryKey : [ '1' ] ,
138123 queryFn : getMyDataStringKey ,
139124 } )
125+
126+ // it should handle query-functions that return Promise<any>
127+ useQuery ( key , ( ) =>
128+ fetch ( 'return Promise<any>' ) . then ( resp => resp . json ( ) )
129+ )
130+
131+ // handles wrapped queries with custom fetcher passed as inline queryFn
132+ const useWrappedQuery = <
133+ TQueryKey extends [ string , Record < string , unknown > ?] ,
134+ TQueryFnData ,
135+ TError ,
136+ TData = TQueryFnData
137+ > (
138+ qk : TQueryKey ,
139+ fetcher : (
140+ obj : TQueryKey [ 1 ] ,
141+ token : string
142+ // return type must be wrapped with TQueryFnReturn
143+ ) => Promise < TQueryFnData > ,
144+ options ?: Omit <
145+ UseQueryOptions < TQueryFnData , TError , TData , TQueryKey > ,
146+ 'queryKey' | 'queryFn'
147+ >
148+ ) => useQuery ( qk , ( ) => fetcher ( qk [ 1 ] , 'token' ) , options )
149+ const test = useWrappedQuery ( [ '' ] , async ( ) => '1' )
150+ expectType < string | undefined > ( test . data )
151+
152+ // handles wrapped queries with custom fetcher passed directly to useQuery
153+ const useWrappedFuncStyleQuery = <
154+ TQueryKey extends [ string , Record < string , unknown > ?] ,
155+ TQueryFnData ,
156+ TError ,
157+ TData = TQueryFnData
158+ > (
159+ qk : TQueryKey ,
160+ fetcher : ( ) => Promise < TQueryFnData > ,
161+ options ?: Omit <
162+ UseQueryOptions < TQueryFnData , TError , TData , TQueryKey > ,
163+ 'queryKey' | 'queryFn'
164+ >
165+ ) => useQuery ( qk , fetcher , options )
166+ const testFuncStyle = useWrappedFuncStyleQuery ( [ '' ] , async ( ) => true )
167+ expectType < boolean | undefined > ( testFuncStyle . data )
140168 }
141169 } )
142170
0 commit comments