Skip to content

fix: pass generic type T through to FetchOptions in interface (#559)#560

Open
guoyangzhen wants to merge 1 commit into
unjs:mainfrom
guoyangzhen:fix/fetch-options-generic-passthrough-559
Open

fix: pass generic type T through to FetchOptions in interface (#559)#560
guoyangzhen wants to merge 1 commit into
unjs:mainfrom
guoyangzhen:fix/fetch-options-generic-passthrough-559

Conversation

@guoyangzhen
Copy link
Copy Markdown

@guoyangzhen guoyangzhen commented Apr 2, 2026

Problem

The `` interface accepts <T, R> generics but only passes `R` to `FetchOptions`, leaving `T` defaulting to `any`. This means `FetchContext` and `FetchResponse` in hooks always have `any` type:

const instance = ofetch.create(defaultOptions)
instance<Data>("", {
  onResponse: context => {
    context.response // is FetchResponse<any> — should be FetchResponse<Data>
  }
})

Fix

Pass T through to FetchOptions in both the main call and raw method:

- options?: FetchOptions<R>
+ options?: FetchOptions<R, T>

This is a pure type-level change with no runtime impact.

Related

Fixes #559

Summary by CodeRabbit

Release Notes

  • Refactor
    • Enhanced the type system for request handling to improve type accuracy, provide better IDE autocompletion, and strengthen type validation across API operations.

…njs#559)

The $Fetch interface accepts <T, R> generics but only passes R to
FetchOptions<R>, leaving T defaulting to any. This means FetchContext
and FetchResponse in hooks always have any type.

Fixed by passing T through: FetchOptions<R> → FetchOptions<R, T>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 09317819-f4d3-47a0-a0d6-ab9be5cfd6cc

📥 Commits

Reviewing files that changed from the base of the PR and between dfbe3ca and 0356aab.

📒 Files selected for processing (1)
  • src/types.ts

📝 Walkthrough

Walkthrough

Updated the $Fetch callable overload signatures to pass the response data type parameter T through to FetchOptions, enabling proper type propagation for request body and callback contexts. Two lines modified in the type declarations.

Changes

Cohort / File(s) Summary
Type Signature Updates
src/types.ts
Updated $Fetch callable overloads to include type parameter T in FetchOptions generic: changed FetchOptions<R> to FetchOptions<R, T> for both default and raw method signatures, enabling type parameter propagation to callbacks and response contexts.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A type that flows from start to end,
Through options broad, a faithful friend,
T now rides where callbacks dwell,
Type safety reigns—all's well, all's well! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: passing the generic type T through to FetchOptions in the $Fetch interface, which is exactly what the code changes do.
Linked Issues check ✅ Passed The PR directly addresses issue #559 by implementing the exact fix required: passing T through FetchOptions<R, T> in both the main call and raw method signatures.
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving the issue: only the two $Fetch callable overload signatures in src/types.ts were modified to pass T through FetchOptions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FetchOptions don't use the first parameter that is supposed to indicate response data

1 participant