Code Monkey home page Code Monkey logo

Comments (7)

surma avatar surma commented on September 26, 2024 1

Yeah, this is a long-standing issue with TypeScript, but this problem already existed with Comlink v3.

FWIW, whenever you use bundlers like Rollup you don’t get these errors.

casting as any is the only solution in this case...

Not true :D That is the reason why I set self to be the default value for expose(), so this will compile just fine:

expose(Markdown);

from comlink.

surma avatar surma commented on September 26, 2024

Good catch, @lacolaco! Thanks for flagging that.

I pushed 4.0.0-alpha.8 which should fix that issue. Can you please confirm?

from comlink.

lacolaco avatar lacolaco commented on September 26, 2024

@surma the original problem seems solved. thank you!
And now I'm in another situation. it looks a breaking change in typing.

ERROR in src/app/worker/markdown.ts(18,18): error TS2345: Argument of type 'Window' is not assignable to parameter of type 'Endpoint'.
  Types of property 'postMessage' are incompatible.
    Type '(message: any, targetOrigin: string, transfer?: Transferable[]) => void' is not assignable to type '(message: any, transfer?: any[]) => void'.
      Types of parameters 'targetOrigin' and 'transfer' are incompatible.
        Type 'any[]' is not assignable to type 'string'.
import * as marked from 'marked';
import { expose } from 'comlink';

export class Markdown {
  compile(source: string) {
    return new Promise<string>((resolve, reject) => {
      marked(source, (err, result) => {
        if (err) {
          reject(err);
          return;
        }
        resolve(result);
      });
    });
  }
}

expose(Markdown, self); // <---- self is not compatible to Endpoint

I think this is not by design, right?

from comlink.

surma avatar surma commented on September 26, 2024

Comlink v4 has loads of breaking changes :D

Before I start: expose() passes self by default now.

But this looks more like TypeScript isn’t aware that you are in a worker context. A simple

declare var self: DedicatedWorkerGlobalScope;

should force TypeScript to assume a worker context.

HTH

from comlink.

lacolaco avatar lacolaco commented on September 26, 2024

It's not easy to solve.

  • DedicatedWorkerGlobalScope is declared in webworker lib.
  • self is declared in both of dom and webworker
    • so, dom and webworker are incompatible

image

declare var self: DedicatedWorkerGlobalScope;

This line is not valid in dom context because DedicatedWorkerGlobalScope is only in webworker context and Window and DedicatedWorkerGlobalScope is not compatible. It's exclusive.

So, if Comlink is used within usual frontend projects, this restriction requires developers to separate tsconfig.json and change transpilation workflow.

from comlink.

lacolaco avatar lacolaco commented on September 26, 2024

casting as any is the only solution in this case...

expose(Markdown, self as any);

from comlink.

lacolaco avatar lacolaco commented on September 26, 2024

@surma I didn't know about the default value! It's perfect for me. Thank you!

The original issue is solved. I close here.

from comlink.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.