Skip to content

TypeScript error: Cannot redeclare block-scoped variable 'self' #443

Closed
@hornta

Description

@hornta

I'm getting a TypeScript error in my service worker file and I can't figure how to solve it.

This is the error I'm getting:

PS C:\project> pnpm lint

> [email protected] lint C:\project
> turbo run lint

• Packages in scope: backend, game, web
• Running lint in 3 packages
• Remote caching disabled
web:lint: cache miss, executing 5eeacbef0868f2c2
web:lint: 
web:lint: > [email protected] lint C:\project
web:lint: > tsc --noEmit && TIMING=1 eslint "src/**/*.ts*"
web:lint: 
web:lint: src/service-worker/sw.ts(3,1): error TS1208: 'sw.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. 
Add an import, export, or an empty 'export {}' statement to make it a module.
web:lint: src/service-worker/sw.ts(3,15): error TS2451: Cannot redeclare block-scoped variable 'self'.
web:lint: src/service-worker/sw.ts(9,9): error TS2339: Property 'waitUntil' does not exist on type 'Event'.
web:lint: src/service-worker/sw.ts(9,49): error TS2339: Property 'notification' does not exist on type 'Event'.
web:lint: src/service-worker/sw.ts(10,9): error TS2339: Property 'notification' does not exist on type 'Event'.
web:lint:  ELIFECYCLE  Command failed with exit code 2.
web:lint: ERROR: command finished with error: command (C:\project\apps\web) pnpm run lint exited (1)
command (C:\project\apps\web) pnpm run lint exited (1)

 Tasks:    0 successful, 1 total
Cached:    0 cached, 1 total
  Time:    3.288s

 ERROR  run failed: command  exited (1)
 ELIFECYCLE  Command failed with exit code 1.

And this is my service worker file:

/// <reference lib="webworker" />

declare const self: ServiceWorkerGlobalScope;

self.addEventListener("install", () => void self.skipWaiting());
self.addEventListener("activate", () => void self.clients.claim());

self.addEventListener("notificationclick", (event) => {
  event.waitUntil(self.clients.openWindow(event.notification.tag));
  event.notification.close();
});

I saw another project put the sw.ts in its own directory in src/service-worker and then put a tsconfig.json at its side. I tried to do it but it doesn't look like it's working.

This is the tsconfig.json src/service-worker/tsconfig.json

{
  "compilerOptions": {
    "lib": ["ESNext", "DOM"],
    "types": ["serviceworker"],
    "allowJs": false,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": false,
    "module": "ESNext",
    "moduleResolution": "Node",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "ESNext",
    "useDefineForClassFields": true
  },
  "include": ["./"]
}

Do you see something obvious wrong or should I provide more information?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions