microsoft / microsoft/TypeScript

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

Aperta
#63,218 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 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.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia con l'esempio Playground collegato e confronta il comportamento descritto nelle issue correlate 4278 e 13347. Analizza la gestione della compatibilità delle interfacce per le proprietà readonly e readwrite; il lavoro è completato quando l'esempio non segnala più l'errore 2320, mentre il comportamento di compatibilità esistente rimane corretto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.