In tsconfig.json
file it is needed to add webworker
library
{
....
"compilerOptions": {
"lib": [
...
"webworker"
],...
}
After adding webworker library it is possible to declare self with typescript ServiceWorkerGlobalScope
.
ServiceWorkerGlobalScope interface of the ServiceWorker API represents the global execution context of a service worker.
declare const self: ServiceWorkerGlobalScope;
If you have set isolatedModules
flag to true in you tsconfig.json then you also need to add some import or export directive to your service worker file, because:
Typescript treats files without import/exports as legacy script files. As such files are not modules and any definitions they have get merged in the global namespace. isolatedModules flag forbids such files.
If you do not need any import or export the handies way to make a file a module is add export { };
stack question describing this problem
At the end it is possible to use full typescript syntax in sw.ts
file.