Skip to content

Commit 53a2d79

Browse files
test(query-core): add test case for setQueryData type inference in functions
1 parent 775d072 commit 53a2d79

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

packages/query-core/src/__tests__/queryClient.test-d.tsx

+38
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ describe('setQueryData', () => {
113113

114114
expectTypeOf(data).toEqualTypeOf<string | undefined>()
115115
})
116+
117+
it('should preserve updater parameter type inference when used in functions with explicit return types', () => {
118+
const queryKey = ['key'] as DataTag<Array<string>, number>
119+
const queryClient = new QueryClient()
120+
121+
// Simulate usage inside a function with explicit return type
122+
// The outer function returns 'unknown' but this shouldn't affect the updater's type inference
123+
;(() =>
124+
queryClient.setQueryData(queryKey, (data) => {
125+
expectTypeOf(data).toEqualTypeOf<number | undefined>()
126+
return data
127+
})) satisfies () => unknown
128+
})
116129
})
117130

118131
describe('getQueryState', () => {
@@ -590,3 +603,28 @@ describe('resetQueries', () => {
590603
})
591604
})
592605
})
606+
type SuccessCallback = () => unknown
607+
it('should infer types correctly with expression body arrow functions', () => {
608+
const queryKey = ['key'] as DataTag<Array<string>, number>
609+
const queryClient = new QueryClient()
610+
611+
// @ts-expect-error
612+
const callbackTest: SuccessCallback = () =>
613+
queryClient.setQueryData(queryKey, (data) => {
614+
expectTypeOf(data).toEqualTypeOf<number | undefined>()
615+
return data
616+
})
617+
})
618+
619+
it('should infer types correctly with block body arrow functions', () => {
620+
const queryKey = ['key'] as DataTag<Array<string>, number>
621+
const queryClient = new QueryClient()
622+
623+
// @ts-expect-error
624+
const callbackTest2: SuccessCallback = () => {
625+
queryClient.setQueryData(queryKey, (data) => {
626+
expectTypeOf(data).toEqualTypeOf<number | undefined>()
627+
return data
628+
})
629+
}
630+
})

0 commit comments

Comments
 (0)