Skip to content

fix(types): avoid excessive instantiation in typed $fetch responses - #36

Merged
pi0 merged 2 commits into
nitrojs:mainfrom
brunobelloni:fix/fetch-types-instantiation
Nov 12, 2026
Merged

pi0 merged 2 commits into
nitrojs:mainfrom
brunobelloni:fix/fetch-types-instantiation

Conversation

@brunobelloni

Copy link
Copy Markdown
Contributor

🔗 Linked issue

None. This is v2-only: Nitro v3 no longer ships the typed internal $fetch types.

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

With many typed routes in InternalApi, calling $fetch from a generic function fails with TS2589: Type instantiation is excessively deep and possibly infinite:

async function getGeneric<T>(url: string) {
  return await $fetch<T>(url)
}
async function getCast<T>(url: string): Promise<T> {
  return (await $fetch(url)) as T
}

With a generic T, the Default extends ... check in TypedInternalResponse is deferred. TypeScript then computes the constraint of the distributive Route extends string ? ... branch, and that means instantiating it for every InternalApi key. AvailableRouterMethod has the same issue.

This PR makes both public aliases non-distributive ([R] extends [string]) and moves the per-route logic into internal helpers that still distribute over each member of a union of routes. For concrete routes the resulting types are the same: the existing fixture type tests pass unchanged. I added a type test for the two generic patterns above.

On a standalone repro with 400 typed routes (TypeScript 6.0.3, tsc --extendedDiagnostics):

errors instantiations check time
before 3 × TS2589 15,596,866 26.9 s
after none 558,956 2.6 s

We hit this in a Nuxt 4 app and have carried the same change as a pnpm patch since October.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Nov 10, 2026

Copy link
Copy Markdown

@brunobelloni is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 19935df7-98b3-481c-b86e-4809ba8ac2cc
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@nitrojs nitrojs temporarily blocked brunobelloni Nov 10, 2026
The fixture has too few routes to trigger the excessive instantiation, so the
generic `$fetch` test passed before the fix too. Add an isolated type check
with 400 typed routes that fails with TS2589 on the previous types.
@pkg-pr-new

pkg-pr-new Bot commented Nov 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitropack@36

commit: 28be28f

@pi0
pi0 merged commit 15de698 into nitrojs:main Nov 12, 2026
8 of 9 checks passed
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.

2 participants