microsoft / microsoft/TypeScript

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

オープン
#63,218 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る
Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
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.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

リンクされた Playground の例から始め、関連する issue 4278 および 13347 で説明されている動作と比較してください。readonly および readwrite プロパティのインターフェイス互換性の処理を調査します。完了条件は、既存の互換性の動作が正しいまま、例でエラー 2320 が報告されなくなることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。