Skip to content

TypeScript: Request constructor should accept Request as second parameter (per Fetch API spec) #5940

@lmaccherone

Description

@lmaccherone

Summary

The generated TypeScript types for the Request constructor don't allow passing a Request object as the second parameter, even though this is valid per the Fetch API specification and works correctly at runtime.

Current Behavior

const original = new Request('https://example.com/old', { method: 'POST', body: 'data' });
const cloned = original.clone();

// ❌ TypeScript error TS2345:
// Argument of type 'Request' is not assignable to parameter of type 'RequestInit'
const newRequest = new Request('https://example.com/new', cloned);

Error:

error TS2345: Argument of type 'Request<CfHostMetadata, Cf>' is not assignable to parameter of type 'RequestInit<CfProperties<unknown>>'.
  Types of property 'cf' are incompatible.
    Type 'Cf | undefined' is not assignable to type 'CfProperties<unknown> | undefined'.

Expected Behavior

The code should compile without error, as it does at runtime. Per the Fetch API spec, the Request constructor accepts either RequestInit or another Request as the second parameter.

Spec Reference

From Fetch Standard § 5.4:

The new Request(input, init) constructor steps are:

  1. Let request be null.
  2. ...
  3. If input is a string, then: ...
  4. Otherwise: Set request to input's request.
  5. ...
  6. If init is given, then: ...

The spec shows that init can be used alongside an existing Request to override specific properties while copying the rest.

Use Case

This pattern is common when you need to clone a request but change its URL:

// Rewrite URL while preserving method, headers, body, etc.
const rewrittenRequest = new Request(newUrl, originalRequest.clone());

Current Workaround

const rewrittenRequest = new Request(newUrl, originalRequest.clone() as RequestInit);

Suggested Fix

Update the Request constructor type signature from:

new <CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>>(
    input: RequestInfo<CfProperties> | URL, 
    init?: RequestInit<Cf>
): Request<CfHostMetadata, Cf>;

To:

new <CfHostMetadata = unknown, Cf = CfProperties<CfHostMetadata>>(
    input: RequestInfo<CfProperties> | URL, 
    init?: RequestInit<Cf> | Request
): Request<CfHostMetadata, Cf>;

Environment

  • Types generated via wrangler types (wrangler 4.44.0+)
  • workerd runtime types from worker-configuration.d.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions