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

Merged

Conversation

ss0526100
Copy link
Contributor

@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
Contributor 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
Contributor 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.

Copy link

nx-cloud bot commented Apr 29, 2025

View your CI Pipeline Execution ↗ for commit 7684df1.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 48s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 23s View ↗

☁️ Nx Cloud last updated this comment at 2025-04-29 07:03:21 UTC

Copy link

pkg-pr-new bot commented Apr 29, 2025

More templates

@tanstack/angular-query-devtools-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@9077

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9077

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9077

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9077

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9077

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@9077

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@9077

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9077

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9077

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@9077

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@9077

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9077

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9077

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@9077

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9077

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9077

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@9077

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9077

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9077

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@9077

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9077

commit: 7684df1

Copy link

codecov bot commented Apr 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.10%. Comparing base (b397fba) to head (7684df1).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #9077       +/-   ##
===========================================
+ Coverage   44.58%   71.10%   +26.51%     
===========================================
  Files         203       19      -184     
  Lines        8104      481     -7623     
  Branches     1812      136     -1676     
===========================================
- Hits         3613      342     -3271     
+ Misses       4059      109     -3950     
+ Partials      432       30      -402     
Components Coverage Δ
@tanstack/angular-query-devtools-experimental ∅ <ø> (∅)
@tanstack/angular-query-experimental ∅ <ø> (∅)
@tanstack/eslint-plugin-query ∅ <ø> (∅)
@tanstack/query-async-storage-persister ∅ <ø> (∅)
@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)
@tanstack/query-codemods ∅ <ø> (∅)
@tanstack/query-core ∅ <ø> (∅)
@tanstack/query-devtools ∅ <ø> (∅)
@tanstack/query-persist-client-core ∅ <ø> (∅)
@tanstack/query-sync-storage-persister ∅ <ø> (∅)
@tanstack/react-query ∅ <ø> (∅)
@tanstack/react-query-devtools ∅ <ø> (∅)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client ∅ <ø> (∅)
@tanstack/solid-query ∅ <ø> (∅)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client ∅ <ø> (∅)
@tanstack/svelte-query ∅ <ø> (∅)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client ∅ <ø> (∅)
@tanstack/vue-query 71.10% <ø> (ø)
@tanstack/vue-query-devtools ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@romansp
Copy link

romansp commented Apr 29, 2025

Thank you for working on the fix.

Sorry if I'm missing something but wouldn't it be more correct to add a new test case that will cover minimal repro I provided in the original issue #9069?

Like literally add a case for useQuery or queryOptions usage where initialData is set to a function which returns TData | undefined. And this should not produce any typescript errors. I just don't see this usage being covered by existing test cases.

@DamianOsipiuk DamianOsipiuk merged commit 66151ca into TanStack:main Apr 29, 2025
8 of 9 checks passed
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
4 participants