microsoft / microsoft/TypeScript
Error (2320) when extending interfaces where a property is both readonly and readwrite.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
### 🔎 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.
Guide de contribution
Ouvrir le guide de contribution
Piste de recherche
Commencez par l’exemple Playground lié et comparez le comportement décrit dans les issues associées 4278 et 13347. Étudiez la gestion de la compatibilité des interfaces pour les propriétés readonly et readwrite ; le travail est considéré comme terminé lorsque l’exemple ne signale plus l’erreur 2320, tout en conservant un comportement de compatibilité existant correct.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100