fix(react-query): improve useQueries select inference#10051
fix(react-query): improve useQueries select inference#10051ganyudedog wants to merge 6 commits intoTanStack:mainfrom
Conversation
Fixes TanStack#6556. Also refactors queryOptions/infiniteQueryOptions overload return types to named *Result aliases to reduce TS4023 'cannot be named' issues for exported options objects (see TanStack#8453).
|
📝 WalkthroughWalkthroughTightens TypeScript inference for Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
TkDodo
left a comment
There was a problem hiding this comment.
I’ve checked both the useQueries and useSuspenseQueries test changes against the main branch and they also don’t yield any errors there.
To verify if your improvements are correct, please show test-cases that fail on main while yielding correct results on your PR. Thanks.
@TkDodo ,I added type-level regression tests that are main-fail/PR-pass. They assert we only infer TData from select when selects input matches TQueryFnData (so unrelated select signatures don't affect inferred results) for both useQueries (QueriesOptions/QueriesResults) and useSuspenseQueries (SuspenseQueriesOptions/SuspenseQueriesResults). Verified with: pnpm -C packages/react-query test:types:tscurrent Thanks. |
Summary
This PR addresses two TypeScript typing issues in
@tanstack/react-query:selectworks differently onuseQueriescompared touseQuery#6556:useQueries/useSuspenseQueriescould lose type inference for theselectcallback input (degrading toany/unknown) when options were provided without explicit generics.queryOptions()/infiniteQueryOptions()could trigger TS4023 ("cannot be named") in consumer declaration output.Changes
useQueriesanduseSuspenseQueriessoTQueryFnDatais derived from thequeryFnreturn type andTDatafrom theselectoutput, restoring correctselect(data)contextual typing.queryOptions/infiniteQueryOptionsoverload return types to named*Resultaliases to make declaration output more stable.selectparameter annotations.Tests
pnpm -C packages/react-query run test:types:tscurrentFixes #6556.
Addresses #8453.
Summary by CodeRabbit
New Features
Improvements
Tests
鉁忥笍 Tip: You can customize this high-level summary in your review settings.
Regression tests (main fail / PR pass)
Adds type-level regression coverage for
useQueriesanduseSuspenseQueriesensuring we only inferTDatafromselectwhenselect's input matchesTQueryFnData.pnpm -C packages/react-query test:types:tscurrent