GoogleChromeLabs / GoogleChromeLabs/comlink
TypeScript typings seem off when wrapping an object graph
- 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
Assessment
This issue has not been assessed yet.