File tree Expand file tree Collapse file tree
preact-query/src/__tests__
react-query/src/__tests__ Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -288,6 +288,23 @@ describe('useQuery', () => {
288288 } )
289289 } )
290290
291+ it ( 'should preserve discriminated-union narrowing' , ( ) => {
292+ type Result =
293+ | { type : 'first' ; first : string }
294+ | { type : 'second' ; second : string }
295+
296+ const query = useQuery ( {
297+ queryKey : queryKey ( ) ,
298+ queryFn : ( ) : Result => ( { type : 'first' , first : 'a' } ) ,
299+ } )
300+
301+ const second = query . data ?. type === 'first' ? undefined : query . data
302+
303+ expectTypeOf ( second ) . toEqualTypeOf <
304+ { type : 'second' ; second : string } | undefined
305+ > ( )
306+ } )
307+
291308 describe ( 'custom hook' , ( ) => {
292309 it ( 'should allow custom hooks using UseQueryOptions' , ( ) => {
293310 type Data = string
Original file line number Diff line number Diff line change @@ -277,6 +277,23 @@ describe('useQuery', () => {
277277 }
278278 } )
279279
280+ it ( 'should preserve discriminated-union narrowing' , ( ) => {
281+ type Result =
282+ | { type : 'first' ; first : string }
283+ | { type : 'second' ; second : string }
284+
285+ const query = useQuery ( {
286+ queryKey : queryKey ( ) ,
287+ queryFn : ( ) : Result => ( { type : 'first' , first : 'a' } ) ,
288+ } )
289+
290+ const second = query . data ?. type === 'first' ? undefined : query . data
291+
292+ expectTypeOf ( second ) . toEqualTypeOf <
293+ { type : 'second' ; second : string } | undefined
294+ > ( )
295+ } )
296+
280297 it ( 'data should not have undefined when initialData is provided' , ( ) => {
281298 const { data } = useQuery ( {
282299 queryKey : queryKey ( ) ,
You can’t perform that action at this time.
0 commit comments