-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
🐛 Bug Report
Description
When using useFetch
with a generic type parameter, the response._data
property in callback hooks like onResponse
loses its type information and falls back to any
, instead of using the specified generic type.
Reproduction
type ApiResponse = {
message: string;
data: { id: number; name: string }[];
};
useFetch<ApiResponse>("/api/v1/users", {
onResponse({ response }) {
if (!response.ok) return;
// ❌ response._data has type `any` instead of `ApiResponse`
doSomethingWithMyData(response._data);
// ^^^^^^^^^^^^^
// Type is `any`, no IntelliSense or type safety
}
});
Expected Behavior
response._data
should be typed as ApiResponse
(the generic type passed to useFetch
), providing proper type safety and IntelliSense.
Actual Behavior
response._data
has type any
, losing all type information and safety.
Root Cause
The issue stems from how Nuxt handles type propagation to the underlying ofetch hooks:
-
ofetch typing: The
_data
property type is defined in ofetch'sFetchResponse
interface (source) -
Type propagation gap: Nuxt extends ofetch's
FetchOptions
type inNitroFetchOptions
, but doesn't forward the response type generics to the underlying ofetch type system -
Fallback to
any
: Without proper type information, ofetch's response hooks default toany
for the_data
property
Environment
- Nuxt version: 4.0.2
- TypeScript version: 5.8.3
Solution
I have already opened a pull request solving this in #32891 as the fix is super small, but I thought I'd open an issue too just to follow protocol and get feedback.
Note: I had Claude rewrite my issue for clarity
Additional information
- Would you be willing to help implement this feature?
- Could this feature be implemented as a module?
Final checks
- Read the contribution guide.
- Check existing discussions and issues.