andywer / threads.js

🧵 Make web workers & worker threads as simple as a function call.
https://threads.js.org/
MIT License
3.06k stars 164 forks source link

Transferable type doesn't exist for Node.js transferables #418

Closed flevi29 closed 2 years ago

flevi29 commented 2 years ago

Hello!

I might be doing something wrong, but I am getting TS2345: Argument of type 'MessagePort' is not assignable to parameter of type 'Transferable'. when providing Transfer with a MessagePort as argument, meanwhile in the description of Transfer it is stated that MessagePort is a valid argument, and it works if I ignore the error. Test here.

andywer commented 2 years ago

I think MessagePort might be part of the dom type library. Try adding "dom" to your tsconfig.json's compilerOptions.lib 😉

flevi29 commented 2 years ago

I added dom to compilerOptions.lib, and it still doesn't work, I would guess because @types/node definition of MessagePort takes priority. Anyhow it wouldn't make sense to add dom, because this is a Node.js application (something I failed to mention yesterday, sorry), and MessagePort is defined a little bit differently. (For example instead of onmessage it has on('message', ( ... ) => { ... })) Here is the extended error I omitted yesterday, sorry about that:

error TS2345: Argument of type 'MessagePort' is not assignable to parameter of type 'Transferable'.
  Type 'MessagePort' is missing the following properties from type 'MessagePort': onmessage, onmessageerror, addEventListener, removeEventListener, dispatchEvent

I believe both definitions of MessagePorts should be supported by the types, I mean it is supported without the types (seemingly, pretty sure).

flevi29 commented 2 years ago

Actually looking at the definitions Transferable is not defined by this awesome package, so maybe this is a @types/node issue or something similar, as I cannot find their own definition of Transferable.

flevi29 commented 2 years ago

Piscina.js solved it like this but it doesn't seem like a solution here, so I guess I'll just Transfer(port1 as any).