-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Open
Description
π Search Terms
Related issues:
- Rules for intersection types are inconsistent with rules for implementing multiple interfaces.Β #4278
- Interface with readonly property is assignable to interface with mutable propertyΒ #13347
π Version & Regression Information
- This is the behavior in every version I tried
β― 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels