mstephen19 / nanolith

Multithreading in Node.js made simple with seamless TypeScript support.
https://www.npmjs.com/package/nanolith
Other
57 stars 2 forks source link

Strict typing for `TaskFunction` #1

Open mstephen19 opened 2 years ago

mstephen19 commented 2 years ago

Currently, the type definition for TaskFunction looks like this:

type TaskFunction = (...args: any[]) => Awaitable<any>;

Though this works, it is problematic because only certain basic data types can be sent to a worker via posting a message. Because the type is any, it does not prevent runtime errors caused by passing in things like functions or random class instances into these functions and expecting it to work.

The TaskFunction type should only accept certain acceptable data types that workers can receive, along with functions like parent.sendMessage or messenger.sendMessage. It must be strict.

mstephen19 commented 2 years ago

@vladfrangu got any ideas on how this could be done?

mstephen19 commented 1 year ago

We can wait for const annotations with TypeScript 5.0 to finally implement this.

mstephen19 commented 1 year ago

Still struggling with this.

gahabeen commented 1 year ago

@mstephen19 Would you mind sharing a minimal example of what you'd like to achieve?