Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type is no longer compatible with fetch #144

Closed
dobesv opened this issue Oct 8, 2020 · 7 comments
Closed

Type is no longer compatible with fetch #144

dobesv opened this issue Oct 8, 2020 · 7 comments

Comments

@dobesv
Copy link

dobesv commented Oct 8, 2020

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'.
@developit
Copy link
Owner

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

@shoota
Copy link

shoota commented Oct 12, 2020

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.

@arjansingh
Copy link

Yup can we please downgrade to the old deps.

@roelkok
Copy link

roelkok commented Nov 10, 2020

I'm having issues too with the types from #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.

@mindplay-dk
Copy link

@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
Copy link

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>;

@developit
Copy link
Owner

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

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

No branches or pull requests

6 participants