GoogleChromeLabs / GoogleChromeLabs/comlink
Proxy not inferred as a promise when it's a nested inside a proxied object as a property value
- Dominant language
- TypeScript
- Stars
- 12.8k
- Forks
- 435
- PR merge metrics
- No merged PRs in 30d
Description
If a situation arises where you need to pass a `proxy()`-ed object to another, the TS types lose track of what should be a Promise.
```ts
// worker
import { expose, proxy } from 'comlink';
const createProxyObject = () => {
const someOtherObject = { /*...*/ };
const newObj = {
someOtherObject: proxy(someOtherObject),
getSomeOtherObject: () => proxy(someOtherObject),
};
return proxy(newObj);
};
expose(createProxyObject);
export type ExposedInterface = typeof createProxyObject;
```
```ts
// main
import { wrap } from 'comlink';
import { ExposedInterface } from './worker';
import MyWorker from './worker?worker';
const worker = new MyWorker();
const wrappedWorker = wrap(worker);
const remoteWorker = await wrappedWorker();
// `someOtherObject` should be `Promise>` 🚫
const otherObject = await remoteWorker.someOtherObject;
// `getSomeOtherObject` properly infers `Promise>` ✅
const objectViaGetter = await remoteWorker.getSomeOtherObject();
```
Example on Stackblitz:
https://stackblitz.com/edit/vitejs-vite-ml9mpl?file=src%2Fmain.ts&file=src%2Fworker.ts&terminal=dev
Contributor guide
Assessment
This issue has not been assessed yet.