Skip to content

Incorrect TypeScript typings for Sentry.registerWebWorker() #17444

@medmunds

Description

@medmunds

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

10.5.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

Sentry.registerWebWorker() is intended to be called from within a worker's implementation. But its TypeScript typings expect a Worker instance, which is only available in the thread that created new Worker(). The type of self within the worker instance will be an extension of WorkerGlobalScope (DedicatedWorkerGlobalScope or ServiceWorkerGlobalScope or SharedWorkerGlobalScope), not Worker.

Suggested fix in webWorker.ts:

  interface RegisterWebWorkerOptions {
-   self: Worker & { _sentryDebugIds?: Record<string, string> };
+   self: WorkerGlobalScope & { _sentryDebugIds?: Record<string, string> };
  }

Steps to Reproduce

  1. npm install @sentry/[email protected]
  2. Create worker.ts:
    import * as Sentry from "@sentry/browser";
    
    Sentry.registerWebWorker({ self });
  3. Ensure the libs in your tsconfig.json are set up to compile that file as a web worker, e.g.:
    { 
      "compilerOptions": { 
        "lib": ["ES2022", "WebWorker"], 
        "...other": "settings..." 
      },
      "include": ["src/worker.ts"]
    }
  4. Attempt to compile the file

Expected Result

No errors

Actual Result

TS2322: Type 'DedicatedWorkerGlobalScope' is not assignable
to type 'Worker & { _sentryDebugIds?: Record<string, string> | undefined; }'.
Property 'terminate' is missing in type 'DedicatedWorkerGlobalScope' but required in type 'Worker'.

Screenshot:
Image

Workaround:

Sentry.registerWebWorker({ self as unknown as Worker });

Metadata

Metadata

Assignees

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions