-
-
Notifications
You must be signed in to change notification settings - Fork 270
(TypeScript) my_worker_1.default is not a constructor #190
Comments
I can avoid the import * as MyWorker from 'worker-loader!./my.worker'; but then I get:
The fix for this problem is to add ...but with the import * as MyWorker from './my.worker'; ...but even then TypeScript complains about the type of
Solution: const worker = new (MyWorker as any)(); |
In tsconfig.json add: {
"compilerOptions": {
"esModuleInterop": true
}
} In // Trickery to fix TypeScript since this will be done by "worker-loader"
export default {} as typeof Worker & (new () => Worker); Now you can use your worker as a normal default import: import MyWorker from './my.worker.ts'; TypeScript now sees {
test: /\.worker.ts$/,
use: [
{
loader: require.resolve('worker-loader'),
options: {
name: 'static/js/[name].js',
publicPath,
},
},
TS_LOADER,
],
},
{
test: /\.tsx?$/,
...TS_LOADER,
}, |
@bfricka Thank you. It works. Except I don't need esModuleInterop in my tsconfig.json |
@steida Yeah, that was an oversight. It's fine since it's a default export. Oops! |
"typescript": "3.1.6",
"webpack": "4.29.1",
"worker-loader": "2.0.0"
The typescript below compiles to
When the app runs,
my_worker_1
evaluates to:An error is therefore thrown:
my_worker_1.default is not a constructor
The code should compile to the following, without reference to
.default
my.worker.ts
app.ts
webpack.config.js
The text was updated successfully, but these errors were encountered: