Skip to content

Commit 4337ce8

Browse files
committed
add union type tests
1 parent cf54a0d commit 4337ce8

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎packages/preact-query/src/__tests__/useQuery.test-d.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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

‎packages/react-query/src/__tests__/useQuery.test-d.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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(),

0 commit comments

Comments
 (0)