Visitar URL original
TypeScript: Incompatible RequestInit and BodyInit types between node-fetch and native fetch API when handling ReadableStream · Issue #1837 · node-fetch/node-fetch · GitHub
Skip to content

TypeScript: Incompatible RequestInit and BodyInit types between node-fetch and native fetch API when handling ReadableStream #1837

Open

Description

@jwerre

Reproduction

Steps to reproduce the behavior:

  1. Create a proxy function that takes a Request object and forwards it to another URL
  2. Try to use node-fetch types (RequestInit, BodyInit) with native fetch API
  3. Observe TypeScript compilation error regarding incompatible body types

Example code:

import type { RequestInit, BodyInit } from 'node-fetch';

export async function proxy(
    request: Request,
    baseUrl = 'https://api.sampleapis.com'
): Promise<Response> {
    const { method, headers, body } = request;
    const url = new URL(request.url);
    const proxyUrl = new URL(`${baseUrl}${url.pathname}`);
    
    const requestConfig: RequestInit = {
        method,
        headers: new Headers(headers),
        body
    };
    
    return fetch(proxyUrl, requestConfig);
}

Expected behavior

The RequestInit and BodyInit types from node-fetch should be compatible with the native fetch API types, allowing for seamless usage between the two. According to the Fetch API specification, the body should be properly typed and compatible across implementations.

Error message received:

Error: No overload matches this call.
  Overload 1 of 2, '(input: string | URL | Request, init?: RequestInit | undefined): Promise<Response>', gave the following error.
    Argument of type 'import("node_modules/node-fetch/@types/index").RequestInit' is not assignable to parameter of type 'RequestInit'.
      Types of property 'body' are incompatible.
        Type 'import("node_modules/node-fetch/@types/index").BodyInit | null | undefined' is not assignable to type 'BodyInit | null | undefined'.
          Type 'ReadableStream' is not assignable to type 'BodyInit | null | undefined'.
  Overload 2 of 2, '(input: URL | RequestInfo, init?: RequestInit | undefined): Promise<Response>', gave the following error.
    Argument of type 'import("node_modules/node-fetch/@types/index").RequestInit' is not assignable to parameter of type 'RequestInit'.
      Types of property 'body' are incompatible.
        Type 'import("node_modules/node-fetch/@types/index").BodyInit | null | undefined' is not assignable to type 'BodyInit | null | undefined'.
          Type 'ReadableStream' is not assignable to type 'BodyInit | null | undefined'. 

	return fetch(proxyUrl, requestConfig);
}

Your Environment

software version
node-fetch 3.3.2
node 20.18.0
npm 10.8.2
Operating System macOS 15.3

Additional context

This appears to be a type compatibility issue between node-fetch's type definitions and the native fetch API types. The specific problem occurs when trying to use node-fetch's RequestInit and BodyInit types with the native fetch implementation, particularly around the handling of ReadableStream in the body property. This makes it difficult to create proxy functions that need to work with both node-fetch and native fetch API types.

The issue specifically manifests when:

  1. Using TypeScript with strict type checking
  2. Attempting to forward requests between node-fetch and native fetch implementations
  3. Working with request bodies, especially streaming bodies

A potential workaround might be to avoid using node-fetch types altogether and use native fetch types, but this isn't always possible in projects that need to maintain compatibility with both APIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions