Skip to content

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

@xenjke

Description

@xenjke

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions