WorkerImplementation<T> Type

Given an interface T that defines the operations provided by a worker, produce an interface to which the implementation of those operations must conform. The return type of each function is enhanced to permit supplying a list of values to be transferred from the worker to the main thread. Multi-argument functions are converted to functions accepting a single tuple of arguments.

  • Every return type R is converted to WorkerReturnType<R>.
  • zeroArgFunc(): R becomes zeroArgFunc(): R | { result: R; transfer: Transferable[]; }.
  • oneArgFunc(arg: U): R becomes oneArgFunc(arg: U): R | { result: R; transfer: Transferable[]; }.
  • multiArgFunc(arg1: U, arg2: V): R becomes multiArgFunc([U, V]): R | { result: R; transfer: Transferable[]; }.

@note All parameters of all methods of T must support structured cloning - attempts to pass functions, instances of classes, DOM nodes, WebGL resources, and other non-cloneable types will compile but fail at run-time.

Defined in

Last Updated: 14 November, 2024