Skip to content

fix: named macros with function syntax now infer previous macro resolve types (#1574)#1691

Merged
SaltyAom merged 1 commit intoelysiajs:mainfrom
raunak-rpm:fix/macro-function-inference-1574
Jan 23, 2026
Merged

fix: named macros with function syntax now infer previous macro resolve types (#1574)#1691
SaltyAom merged 1 commit intoelysiajs:mainfrom
raunak-rpm:fix/macro-function-inference-1574

Conversation

@raunak-rpm
Copy link

@raunak-rpm raunak-rpm commented Jan 20, 2026

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:

.macro("auth", { resolve: () => ({ user: "bob" }) })
.macro("permission", (perm: string) => ({
  auth: true,
  resolve: ({ user }) => { /* user was NOT inferred - TypeScript error! */ }
}))

Root Cause

This was a TypeScript inference limitation - a circular dependency where:

  1. To know the resolve parameter types, TypeScript needs MacroContext
  2. To compute MacroContext, TypeScript needs to know which macros are enabled (from the function's return)
  3. To infer the function's return, TypeScript needs to know the parameter types

Solution

Added a dedicated overload for function-based named macros that:

  1. Computes MacroContext from ALL previous macros (assuming all are enabled)
  2. Properly threads MacroContext['resolve'] to the Singleton type

This approach makes all previous macro resolves available at type-check time since we can't know which ones will be enabled until 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

Test Results

All 1448 tests pass, including the new test case for issue #1574.

Summary by CodeRabbit

  • New Features

    • Named macros now support both function and object syntax declaration forms, providing greater flexibility in macro definition.
  • Tests

    • Added test validation for type inference across chained macro dependencies.

✏️ Tip: You can customize this high-level summary in your review settings.

…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! */ }
  }))
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 20, 2026

Walkthrough

This 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 #1574.

Changes

Cohort / File(s) Summary
Named Macro Type System Enhancements
src/index.ts
Introduces two new overloads for the macro method: one for function-syntax variants and one for object-syntax variants of named macros. Expands the second parameter type to accept `Macro
Test Coverage
test/macro/macro.test.ts
Adds test case "infer previous macro resolve in function syntax (issue #1574)" that validates two chained named macros (auth and permission) where the function-syntax permission macro correctly infers the user type from the prior auth macro's resolve output.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • patch: 1.4.16 #1529: Directly related through modifications to macro method typings and overloads for named-macro support, as well as adjustments to the applyMacro behavior.

Poem

🐰 A function, a name, a type so clear,
No more shall inference disappear,
From auth to permission, resolve flows true,
Where macros chain with types brand new! 🎀

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: enabling named macros with function syntax to infer previous macro resolve types, and references the specific issue #1574.
Linked Issues check ✅ Passed The code changes implement the exact requirement from issue #1574: function-based named macros now properly infer previous macro resolve types through dedicated overloads and improved TypeScript inference.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the named macro function syntax inference issue; modifications to src/index.ts signatures, test additions, and package.json updates are directly aligned with the stated objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 20, 2026

Open in StackBlitz

npm i https://pkg.pr.new/elysiajs/elysia@1691

commit: 37cfac1

@SaltyAom
Copy link
Member

This looks perfect! I have no idea how did I missed this 😭

Thank you so much 😊
Let's get this publish asap!

Koyuki hand

@SaltyAom SaltyAom merged commit be88337 into elysiajs:main Jan 23, 2026
3 checks passed
@SaltyAom
Copy link
Member

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

@SaltyAom
Copy link
Member

Will have to revert this for now

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.

named Macros build with function do not infer previous macro resolve

3 participants