Skip to content

useFetch response hooks lose generic type information, response._data typed as any #32890

@TheColorman

Description

@TheColorman

🐛 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:

  1. ofetch typing: The _data property type is defined in ofetch's FetchResponse interface (source)

  2. Type propagation gap: Nuxt extends ofetch's FetchOptions type in NitroFetchOptions, but doesn't forward the response type generics to the underlying ofetch type system

  3. Fallback to any: Without proper type information, ofetch's response hooks default to any 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions