Tell VSCode that the file is a Web Worker

Viewed 780

I have a web worker that has the two following lines in it:

// Doesn't think importScripts exists
self.importScripts('/myScript.js')

// Thinks that postMessage is missing 1-2 extra arguments
self.postMessage(imgdata)

The code works fine, but the editor thinks that the two items belong on the Window object so it throws errors.

Is there a way to typedoc this or is there something else that I can do so it knows that this file is a web worker?

1 Answers

This one is old, but unanswered. I found the solution, based on this post https://www.antonmata.me/2017/04/04/web-workers-vscode-intellisense.html and the documentation https://code.visualstudio.com/docs/languages/jsconfig#_jsconfig-options

Simply create a jsconfig.json in the directory where the sources of your workers are stored. You can have multiple jsconfigs across your project. The closest jsconfig is the one that is applied to your file.

It basically makes use of the undocumented "lib" option of the "compilerOptions". IntelliSense suggests this, when you edit your jsconfig.json.

jsconfig.json

{
    "compilerOptions": {
        "lib": [
            "webworker"
        ]
    }
}
Related