You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to access ServiceWorkerGlobalScope via self. #14877
Any progress about this? It's kinda sad that we cannot use Service Workers alongside Type Script.
Reacted by syuilo, Jakub Korzeniowski, Ryan Blackman, Andrii Kharsun, Simon Reinisch, KajMagnus, Doug Hoskisson, Emmie Päivärinta, Brandon Bennett, dominictobias and 3 more
Adding declare var self: ServiceWorkerGlobalScope; on top of my service worker implementation fixes that
Reacted by Hossam El-Deen, Marius Volkhart, Peter Radocchia, electrovir, Fabrizio Duroni, Edgar, Emmie Päivärinta, Noah-Jerome Lotzer, hanshou101, Kostiantyn Levytskyi and 28 more
Reacted by Steven Cochrane and SovLyn
Reacted by hanshou101
Reacted by hanshou101
Reacted by hanshou101
Reacted by hanshou101, Kostiantyn Levytskyi and Yoomin Kang
Turns out since I commented, my needs evolved: I needed to define new variables on the global scope.
Since my project mixes a lot of environments, I also had to have many sub .tsconfig files.
We need to cast to unknown first, to remove the error about incompatible types. It is not very pretty, but at least just two lines overall.
This seems to work today without any errors and full Intellisense:
I newer TS version we are still not allowed to redeclare self.
My workaround is: const redeclaredSelf = (self as unknown as ServiceWorkerGlobalScope);
and then using "redeclaredSelf" instead of self.
The answer from Jordan (@jordaaash) was close for me, but I had to tweak it using Pick because I was still getting "not assignable" errors going from WorkerGlobalScope to ServiceWorkerGlobalScope.
So not quite a service worker runtime but follows the same API surface for addEventListener('fetch'). Unfortunately, using /// <reference lib="webworker" /> caused type conflicts with lib DOM and tsconfig.json's jsx mode. Instead I ended up manually defining the type on global:
// Manually define FetchEvent types based on:// https://github.com/microsoft/TypeScript/blob/v5.8.3/src/lib/webworker.generated.d.ts#L2956declare global {interfaceWindowEventMap{fetch: FetchEvent;}}interfaceFetchEventextendsEvent{readonlyrequest: Request;respondWith(r: Response|PromiseLike<Response>): void;}addEventListener('fetch',e=>e.respondWith(myHandler(e.request)));
Would be nice if types like these were more piecemeal and re-composable to avoid referencing the whole webworker context for the subset.
While Window is not directly available to workers, many of the same methods are defined in a shared mixin (WindowOrWorkerGlobalScope), and made available to workers through their own WorkerGlobalScope-derived contexts:
Activity
mhegazy commentedon Mar 27, 2017
I have not found references in the sepec that
selfis of typeServiceWorkerGlobalScope.I think
clientsshould be available as a global variable though. i.e.:mhegazy commentedon Mar 27, 2017
PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.
YuichiNukiyama commentedon Apr 1, 2017
selfproperty will return browsing context object. And according to MDN, sometimes returnServiceWorkerGlobalScopeobject. See following docs.vincentbel commentedon Apr 3, 2017
I'm wondering whether we need to separate typings of web worker and service worker for the following reasons:
clients.closeis not accessible in service worker, how can we omitcloseinServiceWorderGlobalScope?clientsregistrationviaselfin Chrome 57.(Just like theselfin Window)YuichiNukiyama commentedon Apr 6, 2017
FYI #11781
robianmcd commentedon May 2, 2017
As a workaround I am currently doing this
Anyone have a better solution?
Havret commentedon Jun 2, 2017
Any progress about this? It's kinda sad that we cannot use Service Workers alongside Type Script.
mohsen1 commentedon Jun 28, 2017
I did this:
Note you need "webworker" lib with 2.4
abarisain commentedon Sep 2, 2017
Adding
declare var self: ServiceWorkerGlobalScope;on top of my service worker implementation fixes thatjandsu commentedon Sep 29, 2017
Arnaud Barisain-Monrose (@abarisain) I came up with your solution as well but it fails with following error
How did you configure your tsconfig.json to have it working? Thanks!
The workaround I found was:
...but you then have to use
_selfeverywhere :(abarisain commentedon Sep 29, 2017
Are you using the latest typescript version?
Turns out since I commented, my needs evolved: I needed to define new variables on the global scope.
Since my project mixes a lot of environments, I also had to have many sub .tsconfig files.
Here is what works for me:
serviceworker-patches.d.tshas some other patches to fix the incomplete definitionshere is my tsconfig
(the extend part is irrelevant, as all options defined in my base tsconfig are for other stuff)
22 remaining items
Service Worker TS!
sample-system-web-app: Migrate worker files to typescript
falco467 commentedon Mar 10, 2023
Carlos Lopez (@clshortfuse)
We need to cast to unknown first, to remove the error about incompatible types. It is not very pretty, but at least just two lines overall.
This seems to work today without any errors and full Intellisense:
madsskoett commentedon Aug 22, 2023
I newer TS version we are still not allowed to redeclare self.
My workaround is: const redeclaredSelf = (self as unknown as ServiceWorkerGlobalScope);
and then using "redeclaredSelf" instead of self.
jordaaash commentedon Sep 3, 2023
My current favorite workaround:
xaverine commentedon Sep 28, 2023
after adding bun-types to qwik project,
ServiceWorkerGlobalScope type lost it's reference and result in TS compile error.
this hint helped me.
thanhtutzaw commentedon Sep 30, 2023
Cannot find name 'ServiceWorkerGlobalScope'.ts(2304)
type ServiceWorkerGlobalScope =/unresolved/ any
Moving from Better-TypeScript
mikehutter commentedon Nov 22, 2024
The answer from Jordan (@jordaaash) was close for me, but I had to tweak it using Pick because I was still getting "not assignable" errors going from WorkerGlobalScope to ServiceWorkerGlobalScope.
risenxxx commentedon Jan 3, 2025
I used this without modifying tsconfig:
rajsite commentedon Apr 30, 2025
I'm using the WebAssembly jco tools and the JS runtime it uses supports fetch-event.
So not quite a service worker runtime but follows the same API surface for
addEventListener('fetch'). Unfortunately, using/// <reference lib="webworker" />caused type conflicts with libDOMandtsconfig.json'sjsxmode. Instead I ended up manually defining the type on global:Would be nice if types like these were more piecemeal and re-composable to avoid referencing the whole
webworkercontext for the subset.lachieh commentedon Apr 30, 2025
Related: microsoft/TypeScript-DOM-lib-generator#1498
saschanaz commentedon Jun 8, 2025
microsoft/TypeScript-DOM-lib-generator#1498 won't really fix this, the core issue here is that lib=serviceworker should be used instead of webworker.
And with that I think this can be closed.
SirzBenjie commentedon Aug 25, 2025
lib=serviceworkerdoes not seem to be supported. What on earth is webworker if not service worker?falco467 commentedon Sep 16, 2025
Sirz Benjie (@SirzBenjie)
Quoting MDN:
So not every Worker has the Same GlobalScope and so they need different Types to access different APIs.