Skip to content

fix(vue-query): fix initialData Type Inference Bug in useQuery #9077

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

Conversation

ss0526100
Copy link

@ss0526100 ss0526100 commented Apr 28, 2025

Overview

In the previous PR(#9073), there was a bug where data was incorrectly inferred as undefined even when initialData was defined. This PR ensures that when initialData is TData, data is correctly inferred as TData, and adds tests to verify this behavior.

Changes

  • Bug Fix: Improved type inference for non-undefined initialData

    • Modified to ensure data.value is accurately inferred as TData when initialData is provided with a non-undefined value.
    • Commit: 6e73779
  • Test Addition: Type validation for defined initialData

    • Added tests to verify that data is returned as TData type only when initialData is defined.
    • Commit: 2ff92c5, 7684df1

Related Files

  • packages/vue-query/src/useQuery.ts
  • packages/vue-query/src/tests/useQuery.test-d.ts
  • packages/vue-query/src/tests/useQueries.test-d.ts

closes #9069

@saul-atomrigs
Copy link
Contributor

saul-atomrigs commented Apr 28, 2025

I think we also need to rollback this test case:

it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
const { data } = reactive(
useQuery({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => ({
wow: true,
}),
}),
)
expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
})

Initially there was no | undefined in the last line

@ss0526100
Copy link
Author

@saul-atomrigs

As I understand it, when initialData is defined, the type of data should be determined.

In the rollback case you suggested, it seems that even though initialData is defined, it returns an undefined type.

Please explain why this test case should have an undefined value. I don't understand well :(


A test code with the same description as the test for which you suggested a rollback also exists in the React Query tests, and the expected value is defined.

it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
const { data } = useQuery({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => ({
wow: true,
}),
})
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})

@saul-atomrigs
Copy link
Contributor

It should NOT have undefined. We should remove it, like in react-query

@ss0526100
Copy link
Author

@saul-atomrigs

Sorry, I misunderstood that my PR was incorrect.

I've recognized the issue and completed the fix. I'll push the commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[vue-query] Type error returning undefined from initialData in queryOptions
2 participants