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

(types) undici.Response cannot be assigned to Response #3639

Open
panva opened this issue Sep 23, 2024 · 12 comments
Open

(types) undici.Response cannot be assigned to Response #3639

panva opened this issue Sep 23, 2024 · 12 comments
Labels
bug Something isn't working good first issue Good for newcomers Types Changes related to the TypeScript definitions

Comments

@panva
Copy link
Member

panva commented Sep 23, 2024

Bug Description

type undici.Response cannot be assigned to type Response so that module authors can use globally available Response as a type but users may use undici as the implementation to obtain one.

Reproducible By

https://gist.github.com/panva/a350238b643aef7a77b29e6db395c3b9

git clone https://gist.github.com/panva/a350238b643aef7a77b29e6db395c3b9 response-type-reproduction
cd response-type-reproduction
npm i
npx tsc

Expected Behavior

The types should be compatible.

Logs & Screenshots

index.ts:4:3 - error TS2322: Type 'Promise<import("./node_modules/undici/types/fetch").Response>' is not assignable to type 'Promise<Response>'.
  Type 'import("./node_modules/undici/types/fetch").Response' is not assignable to type 'Response'.
    The types returned by 'headers.entries()' are incompatible between these types.
      Type 'SpecIterableIterator<[string, string]>' is missing the following properties from type 'HeadersIterator<[string, string]>': map, filter, take, drop, and 9 more.

4   return undici.fetch(url)
    ~~~~~~


Found 1 error in index.ts:4
@panva panva added the bug Something isn't working label Sep 23, 2024
@panva panva closed this as not planned Won't fix, can't repro, duplicate, stale Sep 23, 2024
@panva panva reopened this Sep 23, 2024
@panva
Copy link
Member Author

panva commented Sep 23, 2024

Okay, regardless of the "target" set in tsconfig.json there's always an error, but the above one is for "ESNext" and there is a different one otherwise, both probably need to be dealt with.

@KhafraDev
Copy link
Member

I think the types are correct here. undici.fetch doesn't return a global response.

@panva
Copy link
Member Author

panva commented Sep 23, 2024

I think the types are correct here. undici.fetch doesn't return a global response.

It's not the global that i'm questioning here, it's the type (well its typescript so really just the TS interfaces), and the those must conform (or extend) the global one's otherwise users cannot use an undici Response in places where the type (interface) of standard Response is expected.

Developers can opt-in to allow global-like objects by not doing an instanceof check and in those cases they will still refer to the TS type Response. But then undici Response cannot be passed solely because of types, not because the runtime not being ready for it.

@panva
Copy link
Member Author

panva commented Sep 23, 2024

It's these helpers typings that are available in node's V8 already that are missing.

@KhafraDev KhafraDev added good first issue Good for newcomers Types Changes related to the TypeScript definitions labels Sep 23, 2024
@hamza-elmoudden

This comment was marked as off-topic.

@panva

This comment was marked as off-topic.

@KhafraDev
Copy link
Member

The issue afaict is that the DOM types replaced the old iterator(s) types with a "HeadersIterator". All we should need to do is implement something similar. Lmk if my understanding of the issue is wrong :)

@hamza-elmoudden

This comment was marked as off-topic.

@panva

This comment was marked as off-topic.

@hamza-elmoudden

This comment was marked as off-topic.

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 1, 2024

I investigated this and there are two issues.

  1. webkitRelativePath is expected to be an attribute of File. This has to be implemented in nodejs and in the types/node package
  2. the esnext iterator helpers are not implemented.

The second one has to be implemented in undici and probably added here:

function iteratorMixin (name, object, kInternalIterator, keyIndex = 0, valueIndex = 1) {

I guess they are trivial changes, if you know how the iterable helper functions work...
Only after those functions are added, we should change the types.

@KhafraDev
Copy link
Member

  1. No it isn't.
  2. They exist. new Headers()[Symbol.iterator]().xyz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers Types Changes related to the TypeScript definitions
Projects
None yet
Development

No branches or pull requests

4 participants