I have a web worker file that shares types with the rest of the typescript application. The web worker file is written in typescript as well.
types.d.ts
export type someType = string | number
search.worker.ts
import type { someType } from 'types.d.ts'
// Web worker logic
app.ts
import type { someType } from 'types.d.ts'
// App logic
I created a separate tsconfig that selectively includes only *.worker.ts and *.d.ts files. I also excluded worker.ts from the main application tsconfig.
The issue is that when I run the worker tsconfig, it emits everything correctly except it treats it as a module. Now inside at the bottom of the transpiled worker.js file there is a export {};.
The issue is that spawning a web worker as a module is not globally supported, and it throws an error if it's there. So exports {}; needs to be removed. I'm not sure how I can accomplish this, or if I need to use other post-bundling tools to get rid of this line.
I suspect that I'm basically telling typescript that it's a module by using that import type { ... line. But I don't really know how to import types otherwise.
Here is my config for tsconfig for the workers:
{
"compilerOptions": {
"outDir": "./assets/workers",
"rootDir": "./src",
"skipLibCheck": true,
"target": "ES2020"
},
"include": [
"./src/**/*.worker.ts",
"./src/**/*.d.ts"
],
}
Thank you!