unfetch icon indicating copy to clipboard operation
unfetch copied to clipboard

Type is no longer compatible with fetch

Open dobesv opened this issue 4 years ago • 6 comments

Unfetch used to declare it's type as typeof fetch which was working well for us.

Now it uses a custom type, which gives us errors when we try to pass it as a parameter to a function expecting something compatible with fetch:

$ tsc --noEmit
src/startup/client/fluentd.ts:10:26 - error TS2345: Argument of type 'Unfetch' is not assignable to parameter of type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
  Types of parameters 'options' and 'init' are incompatible.
    Type 'RequestInit | undefined' is not assignable to type '{ method?: string | undefined; headers?: Record<string, string> | undefined; credentials?: "include" | "omit" | undefined; body?: string | ArrayBuffer | ArrayBufferView | ... 6 more ... | undefined; } | undefined'.
      Type 'RequestInit' is not assignable to type '{ method?: string | undefined; headers?: Record<string, string> | undefined; credentials?: "include" | "omit" | undefined; body?: string | ArrayBuffer | ArrayBufferView | ... 6 more ... | undefined; }'.
        Types of property 'headers' are incompatible.
          Type 'Record<string, string> | Headers | string[][] | undefined' is not assignable to type 'Record<string, string> | undefined'.
            Type 'Headers' is not assignable to type 'Record<string, string>'.
              Index signature is missing in type 'Headers'.

dobesv avatar Oct 08 '20 20:10 dobesv

Ahh, yeah this is an interesting point. We'll likely have to go back to the old defs...

developit avatar Oct 09 '20 21:10 developit

we have similar problem in using apollo-upload-client.

Type 'Unfetch' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
  Types of parameters 'options' and 'init' are incompatible.
    Type 'RequestInit | undefined' is not assignable to type '{ method?: string | undefined; headers?: Record<string, string> | undefined; credentials?: "include" | "omit" | undefined; body?: string | ArrayBuffer | ArrayBufferView | ... 6 more ... | undefined; } | undefined'.
      Type 'RequestInit' is not assignable to type '{ method?: string | undefined; headers?: Record<string, string> | undefined; credentials?: "include" | "omit" | undefined; body?: string | ArrayBuffer | ArrayBufferView | ... 6 more ... | undefined; }'.
        Types of property 'headers' are incompatible.
          Type 'Record<string, string> | Headers | string[][] | undefined' is not assignable to type 'Record<string, string> | undefined'.
            Type 'Headers' is not assignable to type 'Record<string, string>'.
              Index signature is missing in type 'Headers'.

This module using WindowOrWorkerGlobalScope['fetch']; as type.

shoota avatar Oct 12 '20 09:10 shoota

Yup can we please downgrade to the old deps.

arjansingh avatar Oct 13 '20 16:10 arjansingh

I'm having issues too with the types from https://github.com/developit/unfetch/pull/117

UnfetchResponse is not compatible with the fetch Response and is also not exported which makes it hard to use a handler that is not written inline.

roelkok avatar Nov 10 '20 09:11 roelkok

@developit is there any reason we can't just export the types?

I don't want to worry about compatibility issues - I don't use unfetch as a polyfill, so I just want to type-hint against it's own type-definitions, since those accurately reflect what is currently supported by it.

mindplay-dk avatar Jul 23 '21 11:07 mindplay-dk

To others looking for the response type: I managed to break in and steal the promised return-type... yoinks

import type unfetch from "unfetch";

type AsyncReturnType<T extends (...args: any) => any> =
  T extends (...args: any) => Promise<infer U>
    ? U
    : unknown;

export type Response = AsyncReturnType<typeof unfetch>;

mindplay-dk avatar Jul 23 '21 11:07 mindplay-dk

This should be fixed by #159, which is now released as Unfetch 5.0.0.

developit avatar Dec 31 '22 04:12 developit