Skip to content

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

@denis-migdal

Description

@denis-migdal

πŸ”Ž 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions