You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, Comlink.proxy returns just Function type. It is not useful because any language services or editors cannot suggest await new Foo() interface.
My proposal is to define a type of Comlink.proxy function, called PromisedConstructor.
import{Injectable}from'@angular/core';import*asComlinkfrom'comlinkjs';typeLoggerType=import('./logger').Logger;// import only typestypePromised<T>={[PinkeyofT]: T[P]extends(...args: infer A)=> infer R
? (...args: A)=>Promise<R>
: Promise<T[P]>};typePromisedConstructor<T>={new(...args: any): Promise<Promised<T>>;};constLoggerProxy=Comlink.proxy(new(Workerasany)('./logger',{type: 'module'})// global `Worker` workaround...)asPromisedConstructor<LoggerType>;
@Injectable({providedIn: 'root'})exportclassLoggerService{asyncsendLog(value: any){constlogger=awaitnewLoggerProxy();// type safeawaitlogger.log(value);// type-safe}}
I've not found out yet how to extract the original constructor's arguments type. With the above type definition, await new LoggerProxy() 's constructor arguments are all recognized as any[].
Thanks.
The text was updated successfully, but these errors were encountered:
Currently,
Comlink.proxy
returns justFunction
type. It is not useful because any language services or editors cannot suggestawait new Foo()
interface.My proposal is to define a type of
Comlink.proxy
function, calledPromisedConstructor
.TypeScript Playground
Usecase (w/ Angular)
I've not found out yet how to extract the original constructor's arguments type. With the above type definition,
await new LoggerProxy()
's constructor arguments are all recognized asany[]
.Thanks.
The text was updated successfully, but these errors were encountered: