Skip to content

TypeScript typings seem off when wrapping an object graph #680

@jderemer

Description

@jderemer

The web worker that I'm wrapping looks like:

worker.js

interface MyWebWorker {
   service: {
      method: () => void;
   };
}

Comlink.expose({
   service: {
      method: () => {}
   }
})

main.js

const worker = new Worker("worker.js");
  
const obj = Comlink.wrap<MyWebWorker>(worker);

After wrapping in main.js, I get the following typings:

  • obj: Remote<MyWebWorker>
  • obj.service: Promise<{ method: () => void }>

What I would like/expect is the following typings:

  • obj: Remote<MyWebWorker>
  • obj.service: { method: () => void }
  • obj.service.method: () => Promise<void>

At runtime, I can run obj.service.method() and it will successfully make the call to the web worker.

But at compile time, Typescript says Property 'method' does not exist on type 'Promise<{ method: () => void }>'.

My desired typings feel possible based on how it works at runtime, but the typing of Remote<T> seems to just wrap everything in the object graph with promises.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions