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

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

Closed
xenjke opened this issue Mar 24, 2021 · 4 comments
Closed

Comments

@xenjke
Copy link

xenjke commented Mar 24, 2021

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

  • TypeScript: 4.1.2. Also reproducible in 3.8.
  • NodeJS: 14, 12, 10
@xenjke
Copy link
Author

xenjke commented Mar 24, 2021

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

xenjke commented Mar 24, 2021

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!

@xenjke xenjke changed the title Node Response type incompatabile in v3.1.0 Response type incompatabile with pre 4.2 TypeScript, introduced in v3.1.0 Mar 24, 2021
@lquixada
Copy link
Owner

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

xenjke commented Mar 29, 2021

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 👍

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

2 participants