question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typings don't seem to match usage.

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
surmacommented, Jun 15, 2019

I am not sure what your types are, but

const CompareWork = Comlink.wrap<Compare>(workerBase);

means that CompareWork will be an class instance of Compare. My hunch is that you actually want CompareWork to be the class Compare with which you can create new instances using new.

The typical fix for this is

const CompareWork = Comlink.wrap<typeof Compare>(workerBase);
1reaction
surmacommented, Jun 23, 2019

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().

  • If you are using Comlink.expose(MyClass), you are exposing the class and the corresponding counterpart is const thing = Comlink.wrap<typeof MyClass>(worker). thing is now the class (not an instance!), meaning you can’t call any methods (like increment) until you create an instance using const instance = await new thing().
  • If you are using Comlink.expose(new MyClass()), you are exposing an instance and the corresponding counterpart is const 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found