NativeScript / worker-loader

36 stars 20 forks source link

Typescript worker import error "TsWorker is not a constructor" #52

Open belvederef opened 4 years ago

belvederef commented 4 years ago

On I found that importing the typescript worker with

import * as TsWorker from 'nativescript-worker-loader!./workers/bt';

as per the docs does not let the worker to be created and raises the error [TypeError: TsWorker is not a constructor].

The solution is simply to import it using

import TsWorker from 'nativescript-worker-loader!./workers/bt';

I tried both version 0.9.0 and 0.10.0 of this package, and the error is still there when importing with the first method, so I believe the documentation needs to be updated.

dahjelle commented 3 years ago

FWIW, I also got this error in the situation where I (foolishly) imported an enum from within the worker that was defined in the file that imported the worker. In other words,

file.ts

import MyWorker from "nativescript-worker-loader!./worker";

export enum Action {
  first
}

const worker = new MyWorker();
worker.postMessage(…);

worker.ts

import {Action} from "./file";
self.onmessage = function() {
  …
};

I suspect these are two entirely different problems, but since the error message is the same and equally inscrutable, I thought I'd post this here in case it helps someone else.

facetious commented 3 years ago

I am using nativescript-worker-loader@0.11.0 in my project with:

import * as ExampleWorker from 'nativescript-worker-loader!./workers/example.worker';

The typings/worker-loader.d.ts that I've got declares this, which matches the import statement:

declare module "nativescript-worker-loader!*" {
    const content: any;
    export = content;
  }

I think if it declared export default content, then I would have needed to use import ExampleWorker from '...'.