unfetch
unfetch copied to clipboard
Type is no longer compatible with fetch
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'.
Ahh, yeah this is an interesting point. We'll likely have to go back to the old defs...
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.
Yup can we please downgrade to the old deps.
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.
@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.
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>;
This should be fixed by #159, which is now released as Unfetch 5.0.0.