diff --git a/src/comlink.ts b/src/comlink.ts index 27c13694..5a413d96 100644 --- a/src/comlink.ts +++ b/src/comlink.ts @@ -53,9 +53,13 @@ type Unpromisify

= P extends Promise ? T : P; */ type RemoteProperty = // If the value is a method, comlink will proxy it automatically. - // Objects are only proxied if they are marked to be proxied. + // Objects can be either awaited as cloned values or traversed as remote paths. // Otherwise, the property is converted to a Promise that resolves the cloned value. - T extends Function | ProxyMarked ? Remote : Promisify; + T extends Function | ProxyMarked + ? Remote + : T extends object + ? Remote & Promisify + : Promisify; /** * Takes the raw type of a property as a remote thread would see it through a proxy (e.g. when passed in as a function @@ -400,7 +404,7 @@ function closeEndPoint(endpoint: Endpoint) { } export function wrap(ep: Endpoint, target?: any): Remote { - const pendingListeners : PendingListenersMap = new Map(); + const pendingListeners: PendingListenersMap = new Map(); ep.addEventListener("message", function handleMessage(ev: Event) { const { data } = ev as MessageEvent; @@ -643,7 +647,7 @@ function requestResponseMessage( ep.start(); } ep.postMessage({ id, ...msg }, transfers); -}); + }); } function generateUUID(): string { diff --git a/tests/type-checks.ts b/tests/type-checks.ts index 1a33cc55..a0dbbfd4 100644 --- a/tests/type-checks.ts +++ b/tests/type-checks.ts @@ -87,9 +87,27 @@ async function closureSoICanUseAwait() { assert>(false); const subproxy = proxy.c; assert>>(true); + assert }>>(true); assert>(false); const copy = await proxy.c; assert>(true); + + const nestedValue = proxy.c.d; + assert>>(true); + } + + { + interface WorkerApi { + service: { + method: () => void; + }; + } + + const proxy = Comlink.wrap(0 as any); + const call = proxy.service.method(); + assert>>(true); + const service = await proxy.service; + assert void }>>(true); } {