Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 6, 2025

Summary by CodeRabbit

  • New Features

    • Query results in React, Svelte, and Vue now include an optional $optimistic flag on returned data, enabling identification of optimistically updated items.
  • Tests

    • Added/updated type tests to validate presence and correct typing of the $optimistic field across relevant query hooks, including edge cases for paged results.

Copilot AI review requested due to automatic review settings November 6, 2025 03:35
@coderabbitai
Copy link

coderabbitai bot commented Nov 6, 2025

Caution

Review failed

The pull request is closed.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds a generic WithOptimistic<T> type and updates TanStack Query client typings (React, Svelte, Vue) so query result data is wrapped with WithOptimistic<T>. Adds typing tests asserting an optional $optimistic property on query results.

Changes

Cohort / File(s) Summary
Type Definition
packages/clients/tanstack-query/src/utils/types.ts
Add exported generic WithOptimistic<T>: for arrays it maps to Array<Elem & { $optimistic?: boolean }>; for non-array it intersects T & { $optimistic?: boolean }.
React client types
packages/clients/tanstack-query/src/react.ts
Import WithOptimistic and update ModelQueryResult<T> and ModelSuspenseQueryResult<T> to use WithOptimistic<T> as the data type (e.g., UseQueryResult<WithOptimistic<T>, DefaultError>).
Svelte client types
packages/clients/tanstack-query/src/svelte.ts
Import WithOptimistic and update ModelQueryResult<T> to wrap query data with WithOptimistic<T> (e.g., CreateQueryResult<WithOptimistic<T>, DefaultError>).
Vue client types
packages/clients/tanstack-query/src/vue.ts
Import WithOptimistic and update ModelQueryResult<T> to UseQueryReturnType<WithOptimistic<T>, DefaultError> & { queryKey: QueryKey }.
Typing tests
packages/clients/tanstack-query/test/react-typing-test.ts, packages/clients/tanstack-query/test/svelte-typing-test.ts, packages/clients/tanstack-query/test/vue-typing-test.ts
Add assertions and ts-expect-error checks ensuring optional $optimistic appears on useFindFirst / useFindMany results and that certain invalid accesses are flagged for useInfiniteFindMany.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Type-only, consistent pattern applied across three client modules and tests.
  • Minimal logic density; primary change is a conditional mapped type and updating exported type aliases.

Areas requiring attention:

  • Verify WithOptimistic<T> correctly handles nested arrays and union types in consuming code.
  • Ensure all public exported types (including Suspense variants) reflect the new wrapped type consistently.
  • Confirm tests' ts-expect-error cases match the intended type behavior for infinite/paged results.

Possibly related PRs

Poem

🐰 I hop through types with ears held high,
I tuck a little optimism where data lie,
In React, Svelte, Vue the flag may gleam,
A tiny $optimistic in every stream,
Hooray — the queries dream a hopeful dream!

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change—adding $optimistic typing to TanStack query results across multiple client libraries (React, Vue, Svelte).

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d95c3ac and 14fc14c.

📒 Files selected for processing (1)
  • packages/clients/tanstack-query/src/utils/types.ts (1 hunks)

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@claude
Copy link

claude bot commented Nov 6, 2025

Claude finished @ymc9's task —— View job


Code Review Analysis

I've completed my analysis of this PR that fixes $optimistic typing in TanStack query results. Here's my comprehensive review:

Todo List

  • Read and analyze all changed files
  • Review the typing fix implementation
  • Assess code quality and best practices
  • Check for potential bugs or issues
  • Evaluate performance considerations
  • Review security concerns
  • Analyze test coverage
  • Provide comprehensive feedback

✅ Overall Assessment

This is a well-implemented typing fix that correctly addresses the missing $optimistic field in query result types across React, Svelte, and Vue implementations.

📝 Code Quality & Best Practices

Excellent:

  • Consistent implementation across all three framework adapters (React, Svelte, Vue)
  • Proper type composition using the new WithOptimistic<T> utility type
  • Clean separation of concerns with the utility type in utils/types.ts
  • Comprehensive test coverage with both positive and negative test cases

Type Design:

  • The WithOptimistic<T> type handles both array and non-array cases correctly:
    export type WithOptimistic<T> =
        T extends Array<infer U>
            ? Array<U & { $optimistic?: boolean }>
            : T & { $optimistic?: boolean };

🔍 Implementation Details

Key Changes:

  1. New utility type packages/clients/tanstack-query/src/utils/types.ts:34-39 - Properly handles array vs object cases
  2. React integration packages/clients/tanstack-query/src/react.ts:96,101 - Applied to both regular and suspense query results
  3. Svelte integration packages/clients/tanstack-query/src/svelte.ts:94-96 - Wrapped within Readable store type
  4. Vue integration packages/clients/tanstack-query/src/vue.ts:89 - Applied to Vue query return type

🧪 Test Coverage

Comprehensive testing:

  • Positive tests verify $optimistic is accessible on regular query results
  • Negative tests (@ts-expect-error) correctly verify $optimistic is NOT available on infinite queries
  • Consistent patterns across all three frameworks

The infinite query exclusion is intentional and correct - optimistic updates don't apply to paginated data.

🐛 Potential Issues

None identified. The implementation is solid:

  • No breaking changes to existing APIs
  • Type-only changes with no runtime impact
  • Proper handling of edge cases (arrays vs objects)
  • Correct exclusion from infinite queries

⚡ Performance Considerations

Excellent:

  • Zero runtime overhead - this is purely a TypeScript typing improvement
  • No bundle size impact - types are compiled away
  • Efficient type checking - uses conditional types appropriately

🔒 Security Concerns

None. This is a TypeScript-only change with no security implications.

💡 Suggestions

The implementation is already excellent, but minor notes:

  1. Documentation - Consider adding JSDoc comments to the WithOptimistic type explaining its purpose
  2. Future-proofing - The type design is flexible and should handle future enhancements well

🎯 Summary

This PR successfully fixes a typing gap in the TanStack query integration. The $optimistic field is now properly typed across all query result types where it should be available, with appropriate exclusions for infinite queries. The implementation demonstrates good TypeScript practices and thorough testing.

Recommendation: ✅ Approve


Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds TypeScript type support for an $optimistic field on query results to indicate when data is from optimistic updates. The WithOptimistic<T> utility type adds an optional $optimistic?: boolean field to single objects and array elements, but intentionally excludes this field from infinite query results.

  • Introduces a new WithOptimistic<T> utility type that adds $optimistic?: boolean field to objects and array elements
  • Updates ModelQueryResult types across React, Vue, and Svelte to wrap results with WithOptimistic<T>
  • Adds typing tests confirming $optimistic is available on useFindFirst and useFindMany results but NOT on useInfiniteFindMany results

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/clients/tanstack-query/src/utils/types.ts Defines the new WithOptimistic<T> utility type
packages/clients/tanstack-query/src/react.ts Updates ModelQueryResult and ModelSuspenseQueryResult to use WithOptimistic<T>
packages/clients/tanstack-query/src/vue.ts Updates ModelQueryResult to use WithOptimistic<T>
packages/clients/tanstack-query/src/svelte.ts Updates ModelQueryResult to use WithOptimistic<T>
packages/clients/tanstack-query/test/react-typing-test.ts Adds type tests verifying $optimistic field availability on appropriate query results
packages/clients/tanstack-query/test/vue-typing-test.ts Adds type tests verifying $optimistic field availability on appropriate query results
packages/clients/tanstack-query/test/svelte-typing-test.ts Adds type tests verifying $optimistic field availability on appropriate query results
Comments suppressed due to low confidence (3)

packages/clients/tanstack-query/src/react.ts:110

  • The ModelInfiniteQueryResult type is not updated to use WithOptimistic<T> while regular ModelQueryResult and ModelSuspenseQueryResult are. While the test cases confirm this is intentional (using @ts-expect-error to verify $optimistic is not available), the inconsistency could be confusing. Consider adding a code comment explaining why infinite query results intentionally don't include the $optimistic field.
export type ModelInfiniteQueryResult<T> = UseInfiniteQueryResult<T, DefaultError> & { queryKey: QueryKey };

packages/clients/tanstack-query/src/vue.ts:95

  • The ModelInfiniteQueryResult type is not updated to use WithOptimistic<T> while ModelQueryResult is. Consider adding a code comment explaining why infinite query results intentionally don't include the $optimistic field to clarify this design decision for future maintainers.
export type ModelInfiniteQueryResult<T> = UseInfiniteQueryReturnType<T, DefaultError> & { queryKey: QueryKey };

packages/clients/tanstack-query/src/svelte.ts:107

  • The ModelInfiniteQueryResult type is not updated to use WithOptimistic<T> while ModelQueryResult is. Consider adding a code comment explaining why infinite query results intentionally don't include the $optimistic field to maintain consistency with the design decision across all framework adapters.
export type ModelInfiniteQueryResult<T> = Readable<
    UnwrapStore<CreateInfiniteQueryResult<T, DefaultError>> & {
        queryKey: QueryKey;
    }
>;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ymc9 ymc9 merged commit 81924d9 into dev Nov 6, 2025
4 checks passed
@claude
Copy link

claude bot commented Nov 6, 2025

Claude finished @ymc9's task —— View job


Claude finished @ymc9's task —— View job


Code Review Analysis

I've completed my analysis of this PR that fixes $optimistic typing in TanStack query results. Here's my comprehensive review:

Todo List

  • Read and analyze all changed files
  • Review the typing fix implementation
  • Assess code quality and best practices
  • Check for potential bugs or issues
  • Evaluate performance considerations
  • Review security concerns
  • Analyze test coverage
  • Provide comprehensive feedback

✅ Overall Assessment

This is a well-implemented typing fix that correctly addresses the missing $optimistic field in query result types across React, Svelte, and Vue implementations.

📝 Code Quality & Best Practices

Excellent:

  • Consistent implementation across all three framework adapters (React, Svelte, Vue)
  • Proper type composition using the new WithOptimistic<T> utility type
  • Clean separation of concerns with the utility type in utils/types.ts
  • Comprehensive test coverage with both positive and negative test cases

Type Design:

  • The WithOptimistic<T> type handles both array and non-array cases correctly:
    export type WithOptimistic<T> =
        T extends Array<infer U>
            ? Array<U & { $optimistic?: boolean }>
            : T & { $optimistic?: boolean };

🔍 Implementation Details

Key Changes:

  1. New utility type packages/clients/tanstack-query/src/utils/types.ts:34-39 - Properly handles array vs object cases
  2. React integration packages/clients/tanstack-query/src/react.ts:96,101 - Applied to both regular and suspense query results
  3. Svelte integration packages/clients/tanstack-query/src/svelte.ts:94-96 - Wrapped within Readable store type
  4. Vue integration packages/clients/tanstack-query/src/vue.ts:89 - Applied to Vue query return type

🧪 Test Coverage

Comprehensive testing:

  • Positive tests verify $optimistic is accessible on regular query results
  • Negative tests (@ts-expect-error) correctly verify $optimistic is NOT available on infinite queries
  • Consistent patterns across all three frameworks

The infinite query exclusion is intentional and correct - optimistic updates don't apply to paginated data.

🐛 Potential Issues

None identified. The implementation is solid:

  • No breaking changes to existing APIs
  • Type-only changes with no runtime impact
  • Proper handling of edge cases (arrays vs objects)
  • Correct exclusion from infinite queries

⚡ Performance Considerations

Excellent:

  • Zero runtime overhead - this is purely a TypeScript typing improvement
  • No bundle size impact - types are compiled away
  • Efficient type checking - uses conditional types appropriately

🔒 Security Concerns

None. This is a TypeScript-only change with no security implications.

💡 Suggestions

The implementation is already excellent, but minor notes:

  1. Documentation - Consider adding JSDoc comments to the WithOptimistic type explaining its purpose
  2. Future-proofing - The type design is flexible and should handle future enhancements well

🎯 Summary

This PR successfully fixes a typing gap in the TanStack query integration. The $optimistic field is now properly typed across all query result types where it should be available, with appropriate exclusions for infinite queries. The implementation demonstrates good TypeScript practices and thorough testing.

Recommendation: ✅ Approve


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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants