Skip to content
Source
Sign inSign up
TanStack/query

fix(react-query): preserve narrowing with NoInfer #11022

ClosedGujiassh(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/11022/headopened View on GitHub

Showing 2 changed files with 24 additions and 3 deletions.

x
0 of 2 viewed, 2 left
19 additions and 0 deletionspackages/react-query/src/__tests__/useQuery.test-d.tsxModified
View file
@@ -293,6 +293,25 @@
void result
​
+ // eslint-disable-next-line vitest/expect-expect
+ it('should preserve discriminated-union narrowing while keeping the reverse-inference guard', () => {
+ type Result =
+ | { type: 'first'; first: string }
+ | { type: 'second'; second: string }
+​
+ const query = useQuery({
+ queryKey: queryKey(),
+ queryFn: (): Result => ({ type: 'first', first: 'a' }),
+ })
+​
+ const second = query.data?.type === 'first' ? undefined : query.data
+​
+ expectTypeOf(second).toEqualTypeOf<
+ | { type: 'second'; second: string }
+ | undefined
+ >()
+ })
+​
it('data should not have undefined when initialData is provided', () => {
const { data } = useQuery({
queryKey: queryKey(),