-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TS error Excessive stack depth comparing types
when trying to wrap $fetch
#470
Comments
(nuxt 3.2) The problem is related to interface NitroFetchOptions<R extends NitroFetchRequest> extends FetchOptions {
method?: Uppercase<AvailableRouterMethod<R>> | AvailableRouterMethod<R>;
}
interface $Fetch<DefaultT = unknown, DefaultR extends NitroFetchRequest = NitroFetchRequest> {
<T = DefaultT, R extends NitroFetchRequest = DefaultR, O extends NitroFetchOptions<R> = NitroFetchOptions<R>>(request: R, opts?: O): Promise<TypedInternalResponse<R, T, ExtractedRouteMethod<R, O>>>;
raw<T = DefaultT, R extends NitroFetchRequest = DefaultR, O extends NitroFetchOptions<R> = NitroFetchOptions<R>>(request: R, opts?: O): Promise<FetchResponse<TypedInternalResponse<R, T, ExtractedRouteMethod<R, O>>>>;
create<T = DefaultT, R extends NitroFetchRequest = DefaultR>(defaults: FetchOptions): $Fetch<T, R>;
} |
@pi0 can you take a look at this please? Can still reproduce in latest versions. |
Still having this issue with:
|
Same here, problem still present:
|
/cc @danielroe |
It is the same when trying to do with export const useFetchApi = <T>(
...[request, options]: Parameters<typeof useFetch<T>>
): ReturnType<typeof useFetch<T>> => {
return useFetch<T>(request, {
...options,
async onRequest({ options }) {
const accessToken = await useAuthStore().getAccessToken()
if (accessToken) {
const headers = new Headers(options.headers)
headers.set('Accept', 'application/json')
headers.set('Authorization', `Bearer ${accessToken}`)
options.headers = headers
}
},
})
} |
Finally found this workaround for import type {
NitroFetchRequest,
} from "nitropack";
export function $api<
T = unknown,
R extends NitroFetchRequest = 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,
});
} |
having the same issue with that code:
my error: |
For this scenario, have you tried using |
yes I tried. Same error |
@jervalles try this:
Worked for me on Nuxt. |
Have the same problem when using Using Nuxt 3.11.2. |
Yes, this issue is happening in big projects. $fetch<{
item: ISomeItemType
...
}>(...) Using the latest Nuxt 3.11.2 |
workaround by @kenhyuwa work for me, but typecheck of return type not work anymore
|
This issue is still present. The workaround of providing a type to 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. 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:
|
For me still happens, despite I have |
Same for me, whether I try to wrap |
Yes, it happens mostly because the types are complex to be inferred, or because there's lots of routes. |
Same for me. I have a simple login endpoint that returns I made a composable around /**
* 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
*/
export const useFormFetch = async <T>(
fetcher: () => Promise<T>,
form: FormContext
): Promise<Result<T, typeToFlattenedError<T>>> => {
try {
const data = await fetcher();
useFormError(form).clearErrors();
return { success: true, data };
} catch (error: any) {
if (error.data.statusCode !== 400) {
throw error;
}
const errors = error.data.data.errors as typeToFlattenedError<T>;
useFormError(form).setErrors(errors);
return { success: false, error: errors, };
}
}; And here's how I use it: const onSubmit = form.handleSubmit(async (values) => {
const result = await useFormFetch(
() => $fetch('/api/auth/login', { method: 'POST', body: JSON.stringify(values) }),
form
);
}); |
Environment
Darwin
v16.15.1
3.0.0-rc.8
npm@8.11.0
vite
runtimeConfig
,css
,vite
,typescript
,modules
,unocss
@unocss/nuxt@^0.44.7
,@vueuse/nuxt@9.1.1
-
Reproduction
Describe the bug
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 anExcessive 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
The text was updated successfully, but these errors were encountered: