Typings don't seem to match usage.
See original GitHub issueI’ve set up as per the example in the README:
// CompareWorker is loaded through webpack worker-loader
const workerBase = new CompareWorker();
const CompareWork = Comlink.wrap<Compare>(workerBase);
const CompareOr = await new CompareWork();
But when I go to create the instance I get this TS error on line 4:
Cannot use 'new' with an expression whose type lacks a call or construct signature.ts(2351)
The return type from wrap
is an interface with the methods of the class passed through the function. As this doesn’t have a call signature it prompts with an error. TS still compiles fine, but the logged errors are frustrating when the usage is correct.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
Pokémon: These Type Combinations Still Aren't Used
It seems strange that a Normal/Bug-type Pokémon has yet to exist, due to how common the types are and how it would have...
Read more >Unable to get typescript typings to match - Stack Overflow
But the typings don't seem to match when I call the second function. Type 'Error | number[]' is not assignable to type 'number[]'....
Read more >Every Type Combination That Doesn't Exist RANKED - YouTube
There are 17 Type Combinations that don't currently exist in Pokemon - I thought it could be fun to go through and rank...
Read more >Pattern matching and type safety in TypeScript - LogRocket Blog
To address issues that originate from incorrectly handling decisions in the code, we use algebraic data types, a popular concept in functional ...
Read more >Documentation - Do's and Don'ts - TypeScript
❌ Don't use any as a type unless you are in the process of migrating a JavaScript project to TypeScript. The compiler effectively...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am not sure what your types are, but
means that
CompareWork
will be an class instance ofCompare
. My hunch is that you actually wantCompareWork
to be the classCompare
with which you can create new instances usingnew
.The typical fix for this is
For future reference, if you think something is wrong, please put an example on a Glitch. These partial code snippets are not very helpful for me to figure out where things are going wrong. That being said, I think this is all working as intended and you are merely getting your types mixed up a bit 😄
It all depends on what you are passing to
expose()
.Comlink.expose(MyClass)
, you are exposing the class and the corresponding counterpart isconst thing = Comlink.wrap<typeof MyClass>(worker)
.thing
is now the class (not an instance!), meaning you can’t call any methods (likeincrement
) until you create an instance usingconst instance = await new thing()
.Comlink.expose(new MyClass())
, you are exposing an instance and the corresponding counterpart isconst thing = Comlink.wrap<My class>(worker)
.thing
is now an instance and you can call methods straight away.If you still think this is wrong please re-open and give me a full sample showing me the wrong behavior.