GoogleChromeLabs / GoogleChromeLabs/comlink

TypeScript typings seem off when wrapping an object graph

Open
#680 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
12.8k
Forks
435
PR merge metrics
No merged PRs in 30d

Description

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(worker);

```

After wrapping in `main.js`, I get the following typings:
- obj: `Remote`
- obj.service: `Promise<{ method: () => void }>`

What I would like/expect is the following typings:
- obj: `Remote`
- obj.service: `{ method: () => void }`
- obj.service.method: `() => Promise`

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` seems to just wrap everything in the object graph with promises.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.