-
-
Notifications
You must be signed in to change notification settings - Fork 58
Fix: Correct type inference for t.Date() in query parameters
#227
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA new generic utility type Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/treaty2/types.ts (1)
61-69: LGTM! Type transformation correctly handles the common case.The
SerializeQueryParamstype correctly transformsDatefields tostringfor query parameters, which aligns with HTTP query string semantics.Limitations (acceptable for typical query parameter usage):
- Only handles flat/shallow Date fields, not nested objects or Date arrays
- Query parameters are typically flat structures, so this limitation is reasonable for the use case
Optional refinement:
Consider usingRecord<string, unknown>instead ofRecord<string, any>for stricter type safety, though this has minimal practical impact.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/treaty2/types.ts(3 hunks)test/types/treaty2.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/treaty2/types.ts (1)
src/types.ts (1)
MaybeEmptyObject(64-78)
🔇 Additional comments (3)
test/types/treaty2.ts (2)
34-38: LGTM! Clean test case for Date query parameters.The route definition correctly demonstrates the use case: a Date type in the query schema that needs to be handled as a string at the client level.
507-535: LGTM! Type test validates the expected behavior.The test correctly verifies that:
- Client-side query parameters expect
since: string(appropriate for HTTP query strings)- Server-side response data contains
since: Date(after Elysia'st.Date()parsing)This validates the fix achieves its goal of resolving the type mismatch.
src/treaty2/types.ts (1)
89-89: LGTM! SerializeQueryParams correctly applied to both route types.The transformation is properly applied to:
- WebSocket subscribe routes (line 89)
- Regular HTTP routes (line 102)
Both applications correctly wrap the query type with
SerializeQueryParamsbefore passing toMaybeEmptyObject, ensuring Date fields are transformed to strings at the type level for all query parameters.Also applies to: 102-102
Problem
When using
t.Date()in Elysia query schemas, Eden Treaty incorrectly infers that clients should passDateobjects. However, HTTP query parameters are always strings, causing a type mismatch:Solution
SerializeQueryParams<T>type helper insrc/treaty2/types.tstest/types/treaty2.tsClient-side (Eden):
query: { since: string }Server-side (Elysia):
query: { since: Date }Possibly Related Issues
This PR addresses the type-level issue, while previous fixes targeted runtime serialization.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.