fix: named macros with function syntax now infer previous macro resolve types (#1574)#1691
Conversation
…ve types (elysiajs#1574) This commit fixes issue elysiajs#1574 where named macros defined with function syntax did not properly infer the resolve types from previous macros. The fix adds a new overload specifically for function-based named macros that: 1. Computes MacroContext from ALL previous macros (assuming all are enabled) 2. Properly threads the MacroContext['resolve'] to the Singleton type This approach works around a TypeScript inference limitation where the compiler cannot determine which macros are selected from the function's return type before the function's parameter types are resolved. The key insight is that for function syntax, we need to make all previous macro resolves available since we can't know at type-check time which ones will be enabled at runtime. Changes: - Added new overload for function-based named macros in Elysia.macro() - Added dedicated object-syntax overload for clarity - Updated implementation signature to accept functions - Added test case demonstrating the fix Example that now works: .macro('auth', { resolve: () => ({ user: 'bob' }) }) .macro('permission', (perm: string) => ({ auth: true, resolve: ({ user }) => { /* user is now properly inferred! */ } }))
WalkthroughThis PR adds new method overloads for named macros to support function-syntax declarations while improving TypeScript type inference from previously defined macros. The changes enable function-based named macros to properly recognize and type-check references to prior macro contexts, fixing a type inference gap identified in issue Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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 |
commit: |
|
Hold on, this doesn't actually handle type soundness because it assume every macro is enabled Hmm, I'll see what I can do about this |
|
Will have to revert this for now |

Summary
Fixes #1574 - Named macros built with function syntax now properly infer previous macro resolve types.
Problem
When using named macros with function syntax, TypeScript could not properly infer the resolve types from previous macros:
Root Cause
This was a TypeScript inference limitation - a circular dependency where:
resolveparameter types, TypeScript needsMacroContextMacroContext, TypeScript needs to know which macros are enabled (from the function's return)Solution
Added a dedicated overload for function-based named macros that:
MacroContextfrom ALL previous macros (assuming all are enabled)MacroContext['resolve']to the Singleton typeThis approach makes all previous macro resolves available at type-check time since we can't know which ones will be enabled until runtime.
Changes
Elysia.macro()Test Results
All 1448 tests pass, including the new test case for issue #1574.
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.