octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

Typescript build error "cannot find name 'ReadableStream'" #80

Closed lemke-ethan closed 7 months ago

lemke-ethan commented 7 months ago

Hello!

I am getting this error when building my ESM TS project that uses File

node_modules/formdata-node/lib/File-cfd9c54a.d.ts:63:15 - error TS2304: Cannot find name 'ReadableStream'.

63     stream(): ReadableStream<Uint8Array>;
                 ~~~~~~~~~~~~~~

Found 1 error in node_modules/formdata-node/lib/File-cfd9c54a.d.ts:63

I have explicitly excluded node_modules in my tsconfig but that did not help.

Here is a sample project.

This may be related to #33.

Any help would be appreciated. Thank you!

octet-stream commented 7 months ago

Hello. Thanks for reporting the issue!

ReadableStream is one of the web standard streams and it is available in Node.js since v16.5.0 under node:streams/web and from the v18 exposed from globalThis (see history). I have no idea why it's not there in @types/node. My main recommendation is to exclude the code you don't own from type checking via skipLibCheck, because you can always, at any time discover unpredictable errors during types validation due to different configuration in different libraries (not to mention that it will save you some time, but that's not the main problem imo). Note that it will skip all .d.ts files from validation, not only those from node_modules directory. See documentation for more information: https://www.typescriptlang.org/tsconfig/#skipLibCheck

So to fix this issue, you need to enable skipLibCheck option in your tsconfig.json:

{
  "compilerOptions": {
    // ...
    "skipLibCheck": true
  }
}

If skipLibCheck for some reason is not an option, you can either add a .d.ts file with empty ReadableStream declaration in there like this:

declare class ReadableStream<_T> { }

and then add this declaration file to include list.

The 3rd way would be just just include dom in lib option:

{
  "compilerOptions": {
    // ...
    "lib": ["dom"]
  }
}
sam-mfb commented 7 months ago

interestingly, this type actually is in @types/node, but it looks like this library is picking up the dom version for some reason.

ReadableStream in @types/node

sam-mfb commented 7 months ago

digging a little further, it looks like the way this library is setup it is not explicitly importing the ReadableStream type in, for example, Blob.ts, which is causing Typescript to import the dom type.

If there was an interest in making the library stick to only node types (and maybe that's not a priority), I think the node types would probably need to be explicitly imported, e.g., import {ReadableStream} from "stream/web"

octet-stream commented 7 months ago

interestingly, this type actually is in @types/node, but it looks like this library is picking up the dom version for some reason

This class is not exposed on globalThis in @types/node and this is where the issue occurs. In the runtime itself you can access it without importing a module, just like you would in browser. You can see object declarations here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/67ed83d31b2c8c0381b964b0897fc2457b874d11/types/node/globals.d.ts

digging a little further, it looks like the way this library is setup it is not explicitly importing the ReadableStream type in, for example, Blob.ts, which is causing Typescript to import the dom type.

This is intended behavior, because the library itself relies on globalThis. The only place I use Node.js specific APIs is file-from-path utilities, because they're intended for use with Node.js only.

sam-mfb commented 6 months ago

This class is not exposed on globalThis in @types/node and this is where the issue occurs.

Ah. I see. Thanks for the explanation.

sam-mfb commented 6 months ago

i filed an issues on DefinitelyTyped here. Easy enough to workaround, but was curious if there's a reason...

voxpelli commented 6 months ago

Suggested a fix over in https://github.com/octet-stream/form-data-encoder/pull/29, feedback appreciated 🙏

LinusU commented 1 month ago

This was fixed in https://github.com/DefinitelyTyped/DefinitelyTyped/pull/70269