Unable to access ServiceWorkerGlobalScope via `self.`
See original GitHub issueSince 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.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:42
- Comments:30 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 toself
. You’ve still got incorrect types for the global.Here is what worked for me:
tsconfig.json
:Not sure why the empty import is required 🤷♂️