nanolith icon indicating copy to clipboard operation
nanolith copied to clipboard

Strict typing for `TaskFunction`

Open mstephen19 opened this issue 2 years ago • 4 comments

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 avatar Oct 04 '22 22:10 mstephen19

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

mstephen19 avatar Nov 02 '22 10:11 mstephen19

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

mstephen19 avatar Feb 06 '23 22:02 mstephen19

Still struggling with this.

mstephen19 avatar Mar 03 '23 00:03 mstephen19

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

gahabeen avatar Jul 29 '23 10:07 gahabeen