Skip to content

Fetch types are incompatible with "lib/dom" definitions #1943

@kettanaito

Description

@kettanaito

Bug Description

The Request interface exposed by Undici does not satisfy the same type from the global type definitions (lib/dom), and also missing properties according to the Fetch API specification:

export declare class Request implements BodyMixin {

Reproducible By

As an example, consider the following code:

import { Request } from 'undicit'

function transform(req: globalThis.Request) {}

transform(new Request('/hello'))
// ^^^ Property 'referrer' is missing in type 'import(".../node_modules/undici/types/fetch").Request' but required in type 'Request'.ts(2345)

The referrer property is not present in the Undici's Request type definition while it's both required in lib/dom definitions and the Fetch API specification.

readonly attribute [USVString](https://webidl.spec.whatwg.org/#idl-USVString) [referrer](https://fetch.spec.whatwg.org/#dom-request-referrer);

Expected Behavior

Fetch API primitives such as Request, Response and Headers, are compatible with the global

Logs & Screenshots

No applicable logs.

Environment

  • Node: v16.16.0
  • Undici: ^5.20.0
  • TypeScript: ^4.9.4

Additional context

I understand that I'm setting lib/dom as the source of truth here but I expect that shouldn't matter since:

  1. Both Undici and lib/node aim to implement the Fetch API specification.
  2. Undici is objectively missing some properties in its type definitions defined in the spec, like referrer.

Also, for context, I'm spotting this while building Interceptors (mswjs/interceptors#340) where I try using Undici as a Fetch polyfill to guarantee fetch primitives can be created in Node versions prior to global fetch.

I'd like to open a pull request to ensure this compliance after discussing this with the maintainers of this lib.

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypesChanges related to the TypeScript definitionsbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions