question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to access ServiceWorkerGlobalScope via `self.`

See original GitHub issue

Since ServiceWorker related types sit in lib.webworker.d.ts, webworker lib need to be added to tsconfig.json.

{
  "compilerOptions": {
    "lib": [
      "es2017",
      "webworker"
    ]
  }
}

But in current lib.webworker.d.ts, it declare var self: WorkerGlobalScope;, so we can’t access ServiceWorkerGlobalScope via self.


TypeScript Version: 2.2.1

Expected behavior:

Can access ServiceWorkerGlobalScope via self.(like self.clients)

Actual behavior:

Access with error.

image

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:42
  • Comments:30 (5 by maintainers)

github_iconTop GitHub Comments

40reactions
jakearchibaldcommented, May 29, 2019

It feels like TypeScript needs to break the various worker types apart in its definitions. This isn’t just a service worker issue, it’s an issue for other contexts that have a different global, like shared workers & the various worklets.

Either that, or there should be a way to declare which interface is the global.

The various self hacks in this thread only work for references to self. You’ve still got incorrect types for the global.

34reactions
Tetr4commented, Oct 29, 2017

Here is what worked for me:

  • in tsconfig.json:
{
  "compilerOptions": {
    "lib": [
      "es6",
      "webworker",
      "dom"
    ]
  }
}
  • in the service worker:
import { } from ".";
declare var self: ServiceWorkerGlobalScope;

Not sure why the empty import is required 🤷‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to get serviceworker registration object which is typescript ...
Inside your service worker TypeScript file, declare var self: ServiceWorkerGlobalScope; to tell TypeScript that self is an instance of the ...
Read more >
ServiceWorkerGlobalScope - Web APIs - MDN Web Docs
Chrome Edge ServiceWorkerGlobalScope Full support. Chrome40. Toggle history Full support. Edg... abortpayment event. Experimental Full support. Chrome70. Toggle history Full support. Edg... activate event Full support....
Read more >
Service workers - web.dev
They enable fast loading (regardless of the network), offline access, ... can listen for events in the service worker's global scope using the...
Read more >
Service Workers - W3C
A service worker registration has an associated update via cache mode ... To get the service worker object representing serviceWorker (a ...
Read more >
Create a Service Worker with TypeScript - Dev Extent
Unable to access ServiceWorkerGlobalScop via self. Service Worker install Event. The first event listener that is added to the code, listens for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found