From eada2bec2a70791d477895c41a51f26a90e86b61 Mon Sep 17 00:00:00 2001 From: Killian-Aidalinfo Date: Thu, 23 Jul 2026 21:30:36 +0200 Subject: [PATCH] feat(types): opt-in flat NitroFetchRequest to bound $fetch type instantiation Projects with a large generated InternalApi (hundreds of routes) can hit "Type instantiation is excessively deep and possibly infinite" (TS2589) and "Excessive stack depth comparing types" (TS2321) on typed $fetch / useFetch / useAsyncData / event.$fetch, because the NitroFetchRequest route-key union is used as the generic constraint and re-scored at every call site. Add an opt-in NitroFetchConfig interface: when a project sets `{ flatRequest: true }`, NitroFetchRequest resolves to `string`, which stops the per-call-site scoring of the route union. Response typing is preserved (MatchedRoutes still resolves the return from the URL literal); only request-key autocomplete is traded away. Default behavior is unchanged (union), so existing users are unaffected. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/types/fetch/fetch.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/types/fetch/fetch.ts b/src/types/fetch/fetch.ts index 2f0e2dd704..07251a82c5 100644 --- a/src/types/fetch/fetch.ts +++ b/src/types/fetch/fetch.ts @@ -5,14 +5,24 @@ import type { MatchedRoutes } from "./_match.ts"; // An interface to extend in a local project export interface InternalApi {} +// Opt-in configuration interface to augment in a local project, e.g. +// declare module "nitropack" { +// interface NitroFetchConfig { flatRequest: true } +// } +export interface NitroFetchConfig {} + // TODO: upgrade to uppercase for h3 v2 types and web consistency type RouterMethod = Lowercase; -export type NitroFetchRequest = +type TypedNitroFetchRequest = | Exclude | Exclude | (string & {}); +export type NitroFetchRequest = NitroFetchConfig extends { flatRequest: true } + ? string + : TypedNitroFetchRequest; + export type MiddlewareOf< Route extends string, Method extends RouterMethod | "default",