exceeds the maximum length error caused by middleware
#1333
Replies: 3 comments 4 replies
-
|
There’s no fundamental way to avoid the TS7056 “exceeds the maximum length” error in TypeScript when using highly compositional patterns like oRPC’s middleware/context stacking—this is a TypeScript limitation, not an oRPC bug. The best way to prevent or manage it is to use explicit type annotations at key points (like your workaround with For middleware, always use the dependent-context pattern: each middleware should only add the fields it is responsible for, and avoid spreading the entire context (i.e., don’t do If router or procedure types start hitting the limit, split large routers into smaller ones and compose them, or move logic into separate modules with explicit types. You can also explicitly type your handlers using the For monorepos, set Your current workaround and patterns are in line with best practices. If you keep explicit types at boundaries, use dependent-context in middleware, and modularize routers as they grow, you’ll avoid most headaches—even if it’s not a “forever” fix. There’s no “happy path” that avoids this entirely, but these strategies are what oRPC and tRPC maintainers recommend for long-term maintainability ref. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
Referenced discussion in Discord: https://discord.com/channels/1308966753044398161/1456795944321552558 |
Beta Was this translation helpful? Give feedback.
-
|
Because you are using contract-first approach so you only need |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem & Context
Okay first, the error:
And the relevant code (errors on
appRouteFactory):Where
apiFactoryis just the implemented contract:A glimpse of the context definitions:
The contract is still very lightweight:
And the seemingly most problematic part,
appContextProvider(note the use of$context):What I've Read/Tried
https://orpc.dev/docs/advanced/exceeds-the-maximum-length-problem
Read thread https://discord.com/channels/1308966753044398161/1404009710088359957
And thread https://discord.com/channels/1308966753044398161/1399616334760775710
And thread https://discord.com/channels/1308966753044398161/1345370986303848499
Upgraded all packages and apps to Typescript 5.9.3
What Works For Now
Passing the
AppRouteFactoryContextaggregate type directly to theusecall:However, none of the fixes for the "exceeds the maximum length" error seem to be a truly permanent fix. Maybe I don't understand Typescript quite well enough, but splitting up the router and reconstructing it in multiple places doesn't seem optimal to me, and could introduce inconsistencies.
Is there any way to fundamentally avoid this, or maybe a mental frame for preventing it?
How might one split up the router contract (maybe it's not needed because the middleware was most of the problem here, but in the future if the overall "length" needed to be reduced)?
What if the procedures of each router start exceeding the length limit, and it's not optimal to split them into separate routers?
Apologies if any of these questions are irrelevant, I'm just trying to think ahead if this is going to be an issue using oRPC long-term. Kind of hoping for a happy path here.
If I'm using the wrong pattern in any of these places please make a recommendation.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions