microsoft / microsoft/TypeScript

cannot redeclare block scoped variable postMessage()

Open
#55,752 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs More Info
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

"cannot redeclare block scope variable postMessage typescript"

🕗 Version & Regression Information

TS version: ^5.1.6

⏯ Playground Link

Can't use workers in Playground

💻 Code
const worker = new SharedWorker(new URL('./worker.js', import.meta.url))

/* ... */

worker.port.addEventListener('message', e => {
     if (!e.data?.type) {
          return
      }

      switch (e.data.type) {
          case 'get_tabs': {
               worker.port.postMessage({ //Error here
                   type: 'share_tab_id',
                   tab_id: tabId
               })
          }

    /* ... */
🙁 Actual behavior

I'm getting a Cannot redeclare block-scoped variable 'postMessage' error coming from TypeScript itself. I checked the file (lib.dom.d.ts) and it looks like the postMessage variable is getting declared twice

ts-postmessage-error
ts-postmessage-declaration

🙂 Expected behavior

Expect to have single declaration of postMessage and no error.

Additional information about the issue

I'm using a monorepo with NPM workspaces. Each module has a separate tsconfig.json and are built separately

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SharedWorker example and the separate tsconfig.json files in the NPM-workspaces monorepo. Inspect the duplicate postMessage declarations in lib.dom.d.ts and determine why they enter the same TypeScript program; verify the fix by rebuilding the affected modules and confirming the diagnostic is gone.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.