lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 104 forks source link

Response type incompatabile with pre 4.2 TypeScript, introduced in v3.1.0 #102

Closed xenjke closed 3 years ago

xenjke commented 3 years ago

Issue

Version 3.1.0 introduced a breaking change to Response type.

Up to this version, Cross-Fetch response type was matching Response type from TypeScript Types

Example code

import { fetch, Response as CrossFetchResponse } from 'cross-fetch';

fetch('https://www.example').then((response: Response) => {
    console.log(response);
})

Compilation failure

> tsc response-compatibility.ts

response-compatibility.ts:3:35 - error TS2345: Argument of type '(response: Response) => void' is not assignable to parameter of type '(value: Response) => void | PromiseLike<void>'.
  Types of parameters 'response' and 'value' are incompatible.
    Type 'import("cross_fetch_repro/node_modules/cross-fetch/lib.fetch").Response' is not assignable to type 'Response'.
      The types returned by 'body.getReader(...).read(...)' are incompatible between these types.
        Type 'Promise<ReadableStreamDefaultReadResult<Uint8Array>>' is not assignable to type 'Promise<ReadableStreamReadResult<T>>'.
          Type 'ReadableStreamDefaultReadResult<Uint8Array>' is not assignable to type 'ReadableStreamReadResult<T>'.
            Type 'ReadableStreamDefaultReadValueResult<Uint8Array>' is not assignable to type 'ReadableStreamReadResult<T>'.
              Type 'ReadableStreamDefaultReadValueResult<Uint8Array>' is not assignable to type 'ReadableStreamReadValueResult<T>'.
                Types of property 'value' are incompatible.
                  Type 'Uint8Array' is not assignable to type 'T'.
                    'Uint8Array' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'ArrayBufferView'.

3 fetch('https://www.example.com').then((response: Response) => {

Same example compiles fine with cross-fetch locked to 3.0.6.

Environment

xenjke commented 3 years ago

3.1.2 is still failing:

tsc --lib 'es2015,dom' response-compatibility.ts
response-compatibility.ts:3:35 - error TS2345: Argument of type '(response: Response) => Response' is not assignable to parameter of type '(value: Response) => Response | PromiseLike<Response>'.
  Types of parameters 'response' and 'value' are incompatible.
    Type 'import("/Users/ashulzhe/workspace/fetch_repro/node_modules/cross-fetch/lib.fetch").Response' is not assignable to type 'Response'.
      The types returned by 'body.getReader(...).read(...)' are incompatible between these types.
        Type 'Promise<ReadableStreamDefaultReadResult<Uint8Array>>' is not assignable to type 'Promise<ReadableStreamReadResult<T>>'.
          Type 'ReadableStreamDefaultReadResult<Uint8Array>' is not assignable to type 'ReadableStreamReadResult<T>'.
            Type 'ReadableStreamDefaultReadValueResult<Uint8Array>' is not assignable to type 'ReadableStreamReadResult<T>'.
              Type 'ReadableStreamDefaultReadValueResult<Uint8Array>' is not assignable to type 'ReadableStreamReadValueResult<T>'.
                Types of property 'value' are incompatible.
                  Type 'Uint8Array' is not assignable to type 'T'.
                    'Uint8Array' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'ArrayBufferView'.

3 fetch('https://www.example').then((response: Response) => {
                                    ~~~~~~~~~~~~~~~~~~~~~~~~~
xenjke commented 3 years ago

Oh, just saw this line. Looks like those types are from later version of TypeScript.

Tried running with TS 4.2.3:

> node_modules/.bin/tsc -v
Version 4.2.3
> node_modules/.bin/tsc --lib 'dom,es2015' response-compatibility.ts
# compiles!
lquixada commented 3 years ago

@xenjke hey, thanks for reporting this issue. Can you run npm install cross-fetch@3.1.3-alpha.6 and check if the problem persists?

xenjke commented 3 years ago

Hey @lquixada, thank you for the quick turnaround! I can confirm that cross-fetch@3.1.3-alpha.6 resolved the issue for us. We'll pick the fix up from the release 👍