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
Show file tree
Hide file tree
Changes from all commits
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
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
})
}
})
2 changes: 1 addition & 1 deletion packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class QueryClient {
NoInfer<TInferredQueryFnData> | undefined
>,
options?: SetDataOptions,
): TInferredQueryFnData | undefined {
): NoInfer<TInferredQueryFnData> | undefined {
const defaultedOptions = this.defaultQueryOptions<
any,
any,
Expand Down
13 changes: 13 additions & 0 deletions packages/vue-query/src/__tests__/queryClient.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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('fetchInfiniteQuery', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/vue-query/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ export class QueryClient extends QC {
NoInfer<TInferredQueryFnData> | undefined
>,
options?: MaybeRefDeep<SetDataOptions>,
): TInferredQueryFnData | undefined
): NoInfer<TInferredQueryFnData> | undefined
setQueryData<TQueryFnData, TData = NoUnknown<TQueryFnData>>(
queryKey: MaybeRefDeep<QueryKey>,
updater: Updater<NoInfer<TData> | undefined, NoInfer<TData> | undefined>,
options?: MaybeRefDeep<SetDataOptions>,
): TData | undefined
): NoInfer<TData> | undefined
setQueryData<TData>(
queryKey: MaybeRefDeep<QueryKey>,
updater: Updater<TData | undefined, TData | undefined>,
options: MaybeRefDeep<SetDataOptions> = {},
): TData | undefined {
): NoInfer<TData> | undefined {
return super.setQueryData(
cloneDeepUnref(queryKey),
updater,
Expand Down