Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vscode service worker interface objects auto completion

Why vscode dose not see service worker interface objects?
lack of auto completion
While browser shows that they are still there
presence of object logged in browser
How to fix this?

whole service worker code:

self.addEventListener('install', event => {
    console.log('V1 installing…');
    event.waitUntil(
        caches.open('static-v1').then(cache => cache.add('/cat.png'))
    );
});

self.addEventListener('activate', event => {
    console.log('V1 now ready to handle fetches!');
});

self.addEventListener('fetch', event => {
    const url = new URL(event.request.url);
    if (url.origin == location.origin && url.pathname == '/dog.png') {
        event.respondWith(caches.match('/cat.png'));
    }

    if (url.origin == location.origin) {
        console.log("Clients object: ", clients);
    }
});
like image 245
Antony Avatar asked Oct 19 '25 05:10

Antony


1 Answers

The easiest way is to use JSDOC syntax

/**@type {ServiceWorkerGlobalScope} sw */
const sw = self

and use the constant in your code.

sw.addEventListener('install', (ev) => {
  // the event argument will have ServiceWorker specific intellisense
  ev.wait
})

Excludes default libs such as 'dom' conflicting with 'webworker'

On the top of the file add

/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" /> 
like image 75
Cool Lab Avatar answered Oct 21 '25 17:10

Cool Lab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!