unjs / unjs/ofetch

$Fetch.create parametter type

Open
#453 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
5.4k
Forks
195
PR merge metrics
No merged PRs in 30d

Description

Environment

"nuxt": "^3.13.2"
"typescript": "^5.6.3",
"vue-tsc": "^2.1.6"

Reproduction

This is just a type bug that I have in my project, I found a solution that could / seems to fix the bug and wanted to discuss it here

Describe the bug

when using $fetch.create() with the first parameter (defaults: FetchOptions) explicitely defined in my code as of type FetchOptions<'json'> I see an error (pasted in the Log part of this issue)

Additional context

after trying a large number of solutions, I have found that in the $Fetch type definition, the create method ha no generic attached on the first argument.


interface $Fetch {
    <T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R>): Promise<MappedResponseType<R, T>>;
    raw<T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R>): Promise<FetchResponse<MappedResponseType<R, T>>>;
    native: Fetch;
-    create(defaults: FetchOptions, globalOptions?: CreateFetchOptions): $Fetch;
+    create(defaults: FetchOptions<R>, globalOptions?: CreateFetchOptions): $Fetch;
}

this simple change seems to fix my issue. I don't know it this is the right solution though...

Logs
Argument of type '{ responseType: "json"; baseURL: string; body?: RequestInit["body"] | Record<string, any>; ignoreResponseError?: boolean; params?: Record<string, any>; query?: Record<string, any>; ... 24 more ...; onResponseError?: MaybeArray<...> | undefined; }' is not assignable to parameter of type 'FetchOptions<ResponseType, any>'.
  Types of property 'retryDelay' are incompatible.
    Type 'number | ((context: FetchContext<any, "json">) => number) | undefined' is not assignable to type 'number | ((context: FetchContext<any, ResponseType>) => number) | undefined'.
      Type '(context: FetchContext<any, "json">) => number' is not assignable to type 'number | ((context: FetchContext<any, ResponseType>) => number) | undefined'.
        Type '(context: FetchContext<any, "json">) => number' is not assignable to type '(context: FetchContext<any, ResponseType>) => number'.
          Types of parameters 'context' and 'context' are incompatible.
            Type 'FetchContext<any, ResponseType>' is not assignable to type 'FetchContext<any, "json">'.
              Type 'ResponseType' is not assignable to type '"json"'.
                Type '"text"' is not assignable to type '"json"'.

Edit:
I tried this in my project: https://github.com/unjs/ofetch/pull/371

This seams to fix my problem, but can't confirm as of now...

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the $Fetch type definition and reproduce the TypeScript error using $fetch.create() with defaults typed as FetchOptions<'json'>. Review the proposed change in the issue and PR 371, then verify the chosen typing accepts the documented response type without broadening retryDelay types incorrectly. Done means the reported project type-checks successfully and the behavior is covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.