microsoft / microsoft/TypeScript

Duplicate identifier error when merged interface is declared before class

Open
#43,088 1 comment 1 reaction 1 assignee View on GitHub

@sandersn is already working on this.

Since Mar 5, 2021.

Bug Domain: Binder Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

🔎 Search Terms

interface accessors getters declaration merge

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about interfaces, merging, and getters
⏯ Playground Link

Playground link

💻 Code
class Foo {
    get x(): number { return 0; } // ok
}
interface Foo {
    x: number; // ok
}

interface Bar {
    x: number; // not ok
}
class Bar {
    get x(): number { return 0; } // not ok
}
🙁 Actual behavior

Bar throws an error but not Foo

🙂 Expected behavior

Neither or both throw an error (probably both).

While exploring whether an interface can merge with a class in order to narrow the type of a class's inherited getter (there is no documentation, but the answer seems to be that accessor properties are not properly representable in interfaces as of 4.2.2), I came across seemingly unpredictable conditions for what tsc would accept. Eventually, it became apparent that the order of the interface and class declarations has bearing on whether an error is thrown.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.