You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TS error Excessive stack depth comparing types when trying to wrap $fetch #470
In Nuxt 3.0.0-rc.8, when trying to wrap the $fetch method in a custom method with the same type signature (to set default options for example), typescript throw an Excessive stack depth comparing types error, forcing me to add // @ts-ignore on the line before.
This only happens for $fetch. useFetch can be wrapped without errors.
Is it normal ?
Additional context
No response
Logs
No response
Reacted by Jonas Thelemann, Etienne BLANC, Max Mostovoy, Robert Gruner, Max, Nils K, Heb, Seyyed Mahdi, Rasmus Nielsen, Yue JIN and 16 more
Reacted by Hal Heisler, Özüm Eldoğan, Menno Vreman, Jonas Thelemann, Max, Heb, Patrik Sosna, Jules Cordonnier, Alexandre, Antoine Rey and 1 more
Finally found this workaround for $fetch, if anyone is interested :
importtype{NitroFetchRequest,}from"nitropack";exportfunction$api<T=unknown,RextendsNitroFetchRequest=NitroFetchRequest>(request: Parameters<typeof$fetch<T,R>>[0],opts?: Partial<Parameters<typeof$fetch<T,R>>[1]>){return$fetch<T,R>(request,{// add your custom options here
...opts,});}
Reacted by Gerrit van Aaken, Norris Lau, qlash and Benedict Lee
Reacted by Suparerk
Reacted by Luke Nelson, Patrik Sosna, Roberto Bautista, lywoo, tempepe, nicgene and Dominik Klesal
Have the same problem when using $fetch. Strangely I can't seem to reproduce it in a minimal reproduction, so it might have something to do with a large return type or the Nuxt project structure I have. This is a $fetch GET call without any options, body or query.
This issue is still present. The workaround of providing a type to $fetch(), like $fetch<myType>() works, but of course this isn't ideal because we lose automatic type inference. This seems to point towards this automatic inference somehow tripping typescript.
I have tried again and again to reproduce this issue in a minimal app but it just doesn't show up there. It also only shows up for routes of a specific route folder. These routes do deal with numerous deep types inside them, returning large complex JSON data.
I am well aware that this is very difficult to fix if you can't make the error appear anywhere on your end, so it seems we're stuck providing the type hint ourselves for the time being.
this "Excessive stack" error comes and goes, but it's more likely to happen with default GET requests.
Explicitly setting method: 'GET' seems to resolve the issue.
this "Excessive stack" error comes and goes, but it's more likely to happen with default GET requests. Explicitly setting method: 'GET' seems to resolve the issue.
Same for me, whether I try to wrap $fetch or not, and whether I explicitly set the method or not. It happens with Prisma so I suspect the type are indeed to complex to be inferred.
Same for me, whether I try to wrap $fetch or not, and whether I explicitly set the method or not. It happens with Prisma so I suspect the type are indeed to complex to be inferred.
Yes, it happens mostly because the types are complex to be inferred, or because there's lots of routes.
This should be optimized somehow, is annoying always having that error everywhere.
Same for me. I have a simple login endpoint that returns { redirectTo: string }.
I made a composable around $fetch to handle server-side form validation and BadRequest errors with Zod. Here's my code for it:
/** * An helper function that is used to fetch data from the server and handle validation * errors. It is used in conjunction with the useForm composable. * * @param fetcher A function that fetches data from the server, most likely using $fetch * @param form The form context object from vee-validate * @returns A promise that resolves to the data if the fetch was successful, or rejects with an error object */exportconstuseFormFetch=async<T>(fetcher: ()=>Promise<T>,form: FormContext): Promise<Result<T,typeToFlattenedError<T>>>=>{try{constdata=awaitfetcher();useFormError(form).clearErrors();return{success: true, data };}catch(error: any){if(error.data.statusCode!==400){throwerror;}consterrors=error.data.data.errorsastypeToFlattenedError<T>;useFormError(form).setErrors(errors);return{success: false,error: errors,};}};
This either does nothing or somehow causes the vscode to just stop marking the type error, effectively silencing the typecheck. I didn't even notice it at first, thought this workaround succeeded.
And also for some reason IDE lies about the $fetch's awaited return type?
There's another workaround suitable for some cases: using an intermediary const. Infers types, marks errors. Name it data or result and it won't hurt eyes too much.
Activity
ozum commentedon Feb 8, 2023
(nuxt 3.2)
The problem is related to
AvailableRouterMethod<R>type called bymethodattribute inNitroFetchOptionstype definition fromnitropackpackage.daniluk4000 commentedon Jun 16, 2023
@pi0 can you take a look at this please? Can still reproduce in latest versions.
ElYaiko commentedon Jul 16, 2023
Still having this issue with:
gerritvanaaken commentedon Jul 21, 2023
Same here, problem still present:
pi0 commentedon Jul 24, 2023
/cc @danielroe
maxdzin commentedon Jul 24, 2023
It is the same when trying to do with
useFetchoruseAsyncData.For those who stuck with
useFetch, I managed it as this:lacorde commentedon Aug 24, 2023
Finally found this workaround for
$fetch, if anyone is interested :jervalles commentedon Sep 6, 2023
having the same issue with that code:
my error:
Hebilicious commentedon Sep 7, 2023
For this scenario, have you tried using
useFetchinstead ofuseAsyncData? You can pass query parameters too.jervalles commentedon Sep 7, 2023
yes I tried. Same error
ssotomayor commentedon Sep 20, 2023
@jervalles try this:
Worked for me on Nuxt.
kenhyuwa commentedon Jan 7, 2024
finally I wrap
$fetchon nuxt3beejaz commentedon Jan 10, 2024
Im on

Nuxt 3.9.1andVue 3.4.5, I have this error when I use $fetch inside a function. (ignore the redeclare error, just for example)If I move $fetch outside, it doesn't complain

Ragura commentedon Apr 12, 2024
Have the same problem when using
$fetch. Strangely I can't seem to reproduce it in a minimal reproduction, so it might have something to do with a large return type or the Nuxt project structure I have. This is a$fetchGET call without any options, body or query.Using Nuxt 3.11.2.
1 remaining item
mttzzz commentedon Apr 17, 2024
have same issue :(

workaround by @kenhyuwa work for me, but typecheck of return type not work anymore
Ragura commentedon Jul 15, 2024
This issue is still present. The workaround of providing a type to
$fetch(), like$fetch<myType>()works, but of course this isn't ideal because we lose automatic type inference. This seems to point towards this automatic inference somehow tripping typescript.I have tried again and again to reproduce this issue in a minimal app but it just doesn't show up there. It also only shows up for routes of a specific route folder. These routes do deal with numerous deep types inside them, returning large complex JSON data.
I am well aware that this is very difficult to fix if you can't make the error appear anywhere on your end, so it seems we're stuck providing the type hint ourselves for the time being.
minkoonim commentedon Jul 19, 2024
this "Excessive stack" error comes and goes, but it's more likely to happen with default GET requests.
Explicitly setting method: 'GET' seems to resolve the issue.
Steps to Reproduce:
Make a default GET request using $fetch, then another request with a different method (e.g., PUT) with param:
How I got rid of it:
ElYaiko commentedon Nov 13, 2024
For me still happens, despite I have
methoddefined.LouisCassany commentedon Nov 14, 2024
Same for me, whether I try to wrap
$fetchor not, and whether I explicitly set themethodor not. It happens with Prisma so I suspect the type are indeed to complex to be inferred.ElYaiko commentedon Nov 14, 2024
Yes, it happens mostly because the types are complex to be inferred, or because there's lots of routes.
This should be optimized somehow, is annoying always having that error everywhere.
BenocxX commentedon Nov 22, 2024
Same for me. I have a simple login endpoint that returns
{ redirectTo: string }.I made a composable around
$fetchto handle server-side form validation and BadRequest errors with Zod. Here's my code for it:And here's how I use it:
Janibek6119 commentedon Dec 20, 2024
This either does nothing or somehow causes the vscode to just stop marking the type error, effectively silencing the typecheck. I didn't even notice it at first, thought this workaround succeeded.
And also for some reason IDE lies about the
$fetch's awaited return type?There's another workaround suitable for some cases: using an intermediary const. Infers types, marks errors. Name it
dataorresultand it won't hurt eyes too much.pi0 commentedon Jan 7, 2025
Let's track in #2758
Implement Albums core (#51) (#85)
fix: close GDPR erasure gap and fix a dozen more issues from a full a…