Skip to content

fix(query-core, vue-query): fix type inference in setQueryData when used in functions with explicit return types #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(query-core): add test case for setQueryData type inference in fu…
…nctions
  • Loading branch information
custardcream98 committed Apr 16, 2025
commit 53a2d7934508b46c026f3a192c3ed0a75e15a7e0
38 changes: 38 additions & 0 deletions packages/query-core/src/__tests__/queryClient.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ describe('setQueryData', () => {

expectTypeOf(data).toEqualTypeOf<string | undefined>()
})

it('should preserve updater parameter type inference when used in functions with explicit return types', () => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()

// Simulate usage inside a function with explicit return type
// The outer function returns 'unknown' but this shouldn't affect the updater's type inference
;(() =>
queryClient.setQueryData(queryKey, (data) => {
expectTypeOf(data).toEqualTypeOf<number | undefined>()
return data
})) satisfies () => unknown
})
})

describe('getQueryState', () => {
Expand Down Expand Up @@ -590,3 +603,28 @@ describe('resetQueries', () => {
})
})
})
type SuccessCallback = () => unknown
it('should infer types correctly with expression body arrow functions', () => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()

// @ts-expect-error
const callbackTest: SuccessCallback = () =>
queryClient.setQueryData(queryKey, (data) => {
expectTypeOf(data).toEqualTypeOf<number | undefined>()
return data
})
})

it('should infer types correctly with block body arrow functions', () => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()

// @ts-expect-error
const callbackTest2: SuccessCallback = () => {
queryClient.setQueryData(queryKey, (data) => {
expectTypeOf(data).toEqualTypeOf<number | undefined>()
return data
})
}
})