Skip to content

Conversation

@KATT
Copy link
Member

@KATT KATT commented May 28, 2025

Closes #6792
Closes #6791

🎯 Changes

What changes are made in this PR? Is it a feature or a bug fix?

βœ… Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

Summary by CodeRabbit

  • Tests
    • Added a new test to verify correct type inference and behavior when using initial data with queries that return an array of posts.
  • Bug Fixes
    • Improved type inference for queries using initial data, ensuring more accurate data handling in query results.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 28, 2025

Walkthrough

The changes update a generic type usage in the DefinedTRPCQueryOptionsIn interface to use NoInfer for improved type inference and add a new test case to verify correct type inference and runtime behavior when using initialData with a query returning an array.

Changes

Files Change Summary
packages/tanstack-react-query/src/internals/queryOptions.ts Updated generic parameter in DefinedTRPCQueryOptionsIn to use NoInfer<TQueryFnData> in type coercion.
packages/tanstack-react-query/test/queryOptions.test.tsx Added a Post type, a static posts array, a list query procedure, and a test for initialData inference.

Sequence Diagram(s)

sequenceDiagram
    participant TestRunner
    participant ReactComponent
    participant QueryClient
    participant PostRouter

    TestRunner->>ReactComponent: Render with useQuery (post.list, initialData: [])
    ReactComponent->>QueryClient: Fetch post.list query
    QueryClient->>PostRouter: Call list procedure
    PostRouter-->>QueryClient: Return posts array
    QueryClient-->>ReactComponent: Provide data (Post[])
    ReactComponent-->>TestRunner: Render output with post title
Loading

Suggested labels

πŸ‘‹ free for all

Poem

In the warren of types, a new twist appears,
With NoInfer now guiding, we hop without fears.
A test checks our posts, their titles in sight,
Ensuring our queries infer types just right.
πŸ‡βœ¨ Code leaps aheadβ€”oh what a delight!


πŸ“œ Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 95e12af and 8268357.

πŸ“’ Files selected for processing (2)
  • packages/tanstack-react-query/src/internals/queryOptions.ts (1 hunks)
  • packages/tanstack-react-query/test/queryOptions.test.tsx (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/tanstack-react-query/src/internals/queryOptions.ts (1)
packages/server/src/unstable-core-do-not-import/types.ts (1)
  • coerceAsyncIterableToArray (177-178)
⏰ Context from checks skipped due to timeout of 90000ms (18)
  • GitHub Check: E2E-tests (tanstack-start)
  • GitHub Check: E2E-tests (standalone-server)
  • GitHub Check: E2E-tests (vercel-edge-runtime)
  • GitHub Check: E2E-tests (next-formdata)
  • GitHub Check: E2E-tests (next-prisma-starter)
  • GitHub Check: E2E-tests (nuxt)
  • GitHub Check: E2E-tests (next-prisma-todomvc)
  • GitHub Check: E2E-tests (soa)
  • GitHub Check: E2E-tests (next-prisma-websockets-starter)
  • GitHub Check: E2E-tests (next-minimal-starter)
  • GitHub Check: E2E-tests (.experimental/next-app-dir)
  • GitHub Check: E2E-tests (.test/internal-types-export)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
  • GitHub Check: Test a monorepo using built declaration files
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
  • GitHub Check: Release using pkg.pr.new
πŸ”‡ Additional comments (5)
packages/tanstack-react-query/src/internals/queryOptions.ts (1)

58-58: LGTM: Targeted fix for initialData type inference.

The addition of NoInfer<TQueryFnData> correctly prevents TypeScript from incorrectly inferring the query function's return type based on the initialData value. This change is appropriately scoped to only the DefinedTRPCQueryOptionsIn interface, which handles cases where initialData is provided.

packages/tanstack-react-query/test/queryOptions.test.tsx (4)

12-15: Well-structured test type definition.

The Post type is simple and appropriate for testing the initialData inference scenario.


25-25: Good test data setup.

The posts array provides realistic test data for validating both type inference and runtime behavior.


52-52: Simple and effective test procedure.

The list procedure correctly returns the posts array, providing a straightforward way to test the Post[] return type inference.


348-372: Comprehensive test case for initialData inference fix.

This test case effectively validates the NoInfer fix by:

  1. Testing the specific scenario: Using initialData: [] with a query that returns Post[]
  2. Validating type inference: expectTypeOf(query1.data).toEqualTypeOf<Post[]>() ensures the data type is correctly inferred as Post[] rather than being influenced by the empty array initialData
  3. Verifying runtime behavior: Confirming the actual data renders correctly with "Hello world"

This test case directly addresses the issue that the NoInfer change in queryOptions.ts was meant to fix.

✨ Finishing Touches
  • πŸ“ Generate Docstrings

πŸͺ§ Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@vercel
Copy link

vercel bot commented May 28, 2025

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
next-prisma-starter βœ… Ready (Inspect) Visit Preview May 28, 2025 1:55pm
og-image βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback May 28, 2025 1:55pm
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback May 28, 2025 1:55pm

@railway-app
Copy link

railway-app bot commented May 28, 2025

πŸš… Deployed to the trpc-pr-6793 environment in trpc-sse-and-websockets

Service Status Web Updated (UTC)
next-sse-chat ◻️ Removed (View Logs) Web May 28, 2025 at 9:25 pm
next-prisma-websockets-starter ◻️ Removed (View Logs) May 28, 2025 at 9:25 pm

@pkg-pr-new
Copy link

pkg-pr-new bot commented May 28, 2025

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/trpc/trpc/@trpc/client@6793

@trpc/next

npm i https://pkg.pr.new/trpc/trpc/@trpc/next@6793

@trpc/react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/react-query@6793

@trpc/server

npm i https://pkg.pr.new/trpc/trpc/@trpc/server@6793

@trpc/tanstack-react-query

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

@trpc/upgrade

npm i https://pkg.pr.new/trpc/trpc/@trpc/upgrade@6793

commit: 8268357

@KATT KATT changed the title fix(tanstack-react-query): inference of initialData fix(tanstack-react-query): skip inferring initialData May 28, 2025
@KATT KATT merged commit 2afe29a into main May 28, 2025
47 checks passed
@KATT KATT deleted the issues/6792 branch May 28, 2025 21:25
function MyComponent() {
const trpc = useTRPC();
const queryOptions = trpc.post.list.queryOptions(undefined, {
initialData: [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe verify that it works with both initialData and a select which changes the type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did but i'll add a persistent test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i did at least

KATT added a commit that referenced this pull request May 28, 2025
…+ `select` (#6794)

#6793 (comment)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Tests**
- Added a new test to verify correct data type inference when using
initial data with a select function.
- Improved an existing test to directly assert the data type without
early exit conditions.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@github-actions
Copy link
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why isn't queryOptions infer correctly when custom options is passed

3 participants