Skip to content

Commit 66151ca

Browse files
authored
fix(vue-query): fix initialData Type Inference Bug in useQuery (#9077)
* fix: Correct bug where non-undefined values in initialData are not treated as defined * test: Update test to ensure data is TData only when initialData is defined * fix: Modified to recognize UndefinedInitialQueryOptions when a function returning undefined is used in initialData. * test: update test result values for case where a function returning TData is passed to initialData
1 parent b397fba commit 66151ca

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

packages/vue-query/src/__tests__/useQueries.test-d.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('UseQueries config object overload', () => {
3535
})
3636

3737
expectTypeOf(queriesState[0].data).toEqualTypeOf<{ wow: boolean }>()
38-
expectTypeOf(queriesState[1].data).toEqualTypeOf<string | undefined>()
38+
expectTypeOf(queriesState[1].data).toEqualTypeOf<string>()
3939
expectTypeOf(queriesState[2].data).toEqualTypeOf<string | undefined>()
4040
})
4141

@@ -54,9 +54,7 @@ describe('UseQueries config object overload', () => {
5454

5555
const { value: queriesState } = useQueries({ queries: [options] })
5656

57-
expectTypeOf(queriesState[0].data).toEqualTypeOf<
58-
{ wow: boolean } | undefined
59-
>()
57+
expectTypeOf(queriesState[0].data).toEqualTypeOf<{ wow: boolean }>()
6058
})
6159

6260
it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQueries', () => {

packages/vue-query/src/__tests__/useQuery.test-d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('useQuery', () => {
2323
}),
2424
)
2525

26-
expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
26+
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
2727
})
2828

2929
it('TData should be defined when passed through queryOptions', () => {
@@ -40,7 +40,7 @@ describe('useQuery', () => {
4040
})
4141
const { data } = reactive(useQuery(options))
4242

43-
expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
43+
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
4444
})
4545

4646
it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
@@ -74,7 +74,7 @@ describe('useQuery', () => {
7474
}),
7575
)
7676

77-
expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
77+
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
7878
})
7979

8080
it('TData should have undefined in the union when initialData is NOT provided', () => {

packages/vue-query/src/useQuery.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useBaseQuery } from './useBaseQuery'
33
import type {
44
DefaultError,
55
DefinedQueryObserverResult,
6-
InitialDataFunction,
76
QueryKey,
87
QueryObserverOptions,
98
} from '@tanstack/query-core'
@@ -65,10 +64,7 @@ export type UndefinedInitialQueryOptions<
6564
TData = TQueryFnData,
6665
TQueryKey extends QueryKey = QueryKey,
6766
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey> & {
68-
initialData?:
69-
| undefined
70-
| InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
71-
| NonUndefinedGuard<TQueryFnData>
67+
initialData?: undefined | (() => undefined)
7268
}
7369

7470
export type DefinedInitialQueryOptions<

0 commit comments

Comments
 (0)