Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy not inferred as a promise when it's a nested inside a proxied object as a property value #634

Open
ivancuric opened this issue Mar 20, 2023 · 4 comments

Comments

@ivancuric
Copy link

ivancuric commented Mar 20, 2023

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.

// 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;
// main
import { wrap } from 'comlink';
import { ExposedInterface } from './worker';
import MyWorker from './worker?worker';

const worker = new MyWorker();

const wrappedWorker = wrap<ExposedInterface>(worker);

const remoteWorker = await wrappedWorker();

// `someOtherObject` should be `Promise<Remote<...>>` 🚫
const otherObject = await remoteWorker.someOtherObject;

// `getSomeOtherObject` properly infers `Promise<Remote<...>>` ✅
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

@ghost
Copy link

ghost commented Mar 24, 2023

Bro, I don't want to advertise, but you'd better try my InterCom and InterWork from the Blazer package (in Github). I myself did not like Google Comlink for many things (including problems with ImageBitmap, Blob and other objects, as well as the above mentioned reasons), and decided to write my own library. Leave this company alone. Yeah, you wouldn't believe it, I did the impossible.

@ivancuric
Copy link
Author

Bro, I don't want to advertise, but you'd better try my InterCom and InterWork from the Blazer package (in Github). I myself did not like Google Comlink for many things (including problems with ImageBitmap, Blob and other objects, as well as the above mentioned reasons), and decided to write my own library. Leave this company alone. Yeah, you wouldn't believe it, I did the impossible.

This sounds so toxic that I'm certain that I won't.

@benjamind
Copy link
Collaborator

For what it's worth, neither Surma, nor myself work for Google!

@ivancuric
Copy link
Author

Any ideas what could be done here? Or should I just assert types manually?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants