How to have the language server in Visual Studio Code recognize that a JavaScript file is meant to be used as service worker?

3 weeks ago 27
ARTICLE AD BOX

For example, given a single file service_worker.mjs with these contents:

self.addEventListener("install", () => self.skipWaiting()); self.addEventListener("activate", () => self.clients.claim());

In Visual Studio Code, skipWaiting and clients have red squiggly underlines as if there are syntax errors and show error messages when I hover over the words:

Property 'skipWaiting' does not exist on type 'Window & typeof globalThis'.

Property 'clients' does not exist on type 'Window & typeof globalThis'.

Normally that would be correct, but I would like to use that file as a service worker, so I think that self should be treated as ServiceWorkerGlobalScope by the language server. How do I get the language server in Visual Studio Code to recognize self as ServiceWorkerGlobalScope?

Screenshot of code and "problems" tab in Visual Studio Code:

Read Entire Article