microsoft / microsoft/TypeScript

Error (2320) when extending interfaces where a property is both readonly and readwrite.

未关闭
#63,218 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

Related issues:
- https://github.com/microsoft/TypeScript/issues/4278
- https://github.com/microsoft/TypeScript/issues/13347

### 🕗 Version & Regression Information

- This is the behavior in every version I tried

### ⏯ Playground Link

[Playground Link](https://www.typescriptlang.org/play/?ts=6.0.0-dev.20260224#code/JYOwLgpgTgZghgYwgAgILIN4Chm+VCOAEwHsQAbAT2RhJIC5kQBXAWwCNoBuLAXy1CRYiFACFMOPLQZM2nKD34Dw0eEmQBhZBAAekEEQDOaADTJxGXkA)

### 💻 Code

```ts
interface A { readonly foo: number; }
interface B { foo: number; }

interface C extends A, B {}
```

### 🙁 Actual behavior

```ts
Interface 'C' cannot simultaneously extend types 'A' and 'B'.
Named property 'foo' of types 'A' and 'B' are not identical.(2320)
```

### 🙂 Expected behavior

No error.

### Additional information about the issue

As far as I understand, `readonly` isn't a constraint, but an interface specification.
Meaning, that `readonly foo: number` is almost equivalent to:
```ts
get foo(): number;
```
Not to:
```ts
get foo(): number;
set foo(_: never): void; // If I'm not mistaken this should forbid assignations.
```

_(TBH, a `read` keyword would be less confusing than `readonly`.)_

Therefore, here, I want to merge:
```ts
get foo(): number;
```
With:
```ts
get foo(): number;
set foo(f: number): void;
```

We can see it as:
- "merging" 2 identical getters, which cause no issue.
- adding an additional property (like we could add a method) : the setter.

However, the latter causes an issue when it shouldn't. This is just adding something.

Note: TS requires the 2 types of `foo` to be strictly identical.
_But_, when one is `readonly`, then it could accept the second to be a specialization of it (i.e. assignable to the `readonly` version).

Indeed, the resulting interface would still be assignable to the first one.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 Playground 示例开始,并比较相关 issue 4278 和 13347 中描述的行为。调查 readonly 和 readwrite 属性的接口兼容性处理;完成的标准是示例不再报告错误 2320,同时现有的兼容性行为仍然正确。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。