microsoft / microsoft/TypeScript

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

Offen
#63,218 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔎 Search Terms

Related issues:

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground Link

💻 Code
interface A { readonly foo: number; }
interface B { foo: number; }

interface C extends A, B {}
🙁 Actual behavior
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:

get foo(): number;

Not to:

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:

get foo(): number;

With:

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten Playground-Beispiel und vergleiche das in den verwandten Issues 4278 und 13347 beschriebene Verhalten. Untersuche die Behandlung der Schnittstellenkompatibilität für readonly- und readwrite-Eigenschaften; abgeschlossen ist die Arbeit, wenn das Beispiel nicht mehr den Fehler 2320 meldet, während das bestehende Kompatibilitätsverhalten korrekt bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.