3

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:

asked 2 days ago
2
  • 1
    I believe this should solve it! stackoverflow.com/questions/46493539/… ``` // Source - stackoverflow.com/a/50823389 // Posted by htho // Retrieved 2026年01月04日, License - CC BY-SA 4.0 { "compilerOptions": { "lib": [ "webworker" ] } } ``` Commented 2 days ago
  • @Sophia, thanks, that helped me understand the issue further, although that link is for Web Workers, which I think has a different API than Service Workers. I settled on this approach for now: const serviceWorker = /** @type {ServiceWorkerGlobalScope} */ (/** @type {unknown} */ (globalThis.self));. Commented 2 days ago

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.