fix(rest): Guard RestEndpoint against O=any in subclassed constructors#3847
fix(rest): Guard RestEndpoint against O=any in subclassed constructors#3847
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
|
Size Change: 0 B Total Size: 80.9 kB ℹ️ View Unchanged
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3847 +/- ##
=======================================
Coverage 98.08% 98.08%
=======================================
Files 152 152
Lines 2871 2871
Branches 563 563
=======================================
Hits 2816 2816
Misses 11 11
Partials 44 44 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Add `unknown extends O ? any :` before the searchParams conditional in RestEndpoint<O> and RestEndpointConstructorOptions<O>. When subclassing with `O extends RestGenerics = any`, this catches O=any before it reaches PathArgs, preventing the restrictive index-signature type. Includes detailed comment explaining the partial inference limitation where TypeScript may widen path literals to `string` due to complex conditional constructor parameter types. Follows up on #3845 which fixed PathArgs<any> but missed the higher-level propagation through RestEndpointTypes. Made-with: Cursor
13531fd to
68b8b29
Compare
Follows up on #3845.
Motivation
#3845 fixed
PathArgs<any>returning a restrictive index-signature type, but theanyalso propagates through the searchParams conditional inRestEndpoint<O>andRestEndpointConstructorOptions<O>. When subclassing withO extends RestGenerics = any, the[O['searchParams']] extends [undefined]non-distributive wrapper (added in #3782 for TS6) preventsanyfrom passing through, producing the same restrictive type at a higher level.Solution
Add
unknown extends O ? any :guard before the searchParams conditional in bothRestEndpoint<O>andRestEndpointConstructorOptions<O>. This catchesO = anybefore it reachesPathArgs, so subclassed endpoints accept concrete body types.Includes a detailed comment explaining a known limitation: TypeScript may partially infer
Ofrom constructor arguments, widening path literals (e.g.,'/users/:id') tostring(theRestGenericsconstraint) instead of preserving the literal. This is inherent to TypeScript's class constructor inference with complex conditional parameter types and would require restructuring the constructor options type to fix.Open questions
N/A
Made with Cursor
Note
Low Risk
Low risk: this is a TypeScript type-level change plus additional type tests; runtime behavior is unaffected, but it could subtly change inference/acceptance for code that relied on previous
anypropagation.Overview
Fixes a TypeScript typing regression where subclassing
RestEndpointwithO extends RestGenerics = anycould causeanyto be blocked by thesearchParamsconditional and yield overly restrictive URL-param/body types.Adds an
unknown extends O ? any : ...guard to bothRestEndpointandRestEndpointConstructorOptionsto restore expectedanybehavior, and expands the TypeScript test to cover subclassed endpoints with both param and static paths (GET/POST) to ensure concrete body types remain accepted.Written by Cursor Bugbot for commit 68b8b29. This will update automatically on new commits. Configure here.