-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error "TS2320" when interface extends interface that has optional member and another interface that has same named required member #3598
Comments
However if I do: interface C extends A, B {
member1: string;
} it works. but it doesn't work when I do: interface C extends A, B {
member1?: string;
} |
Users might be better served if the message said something like "Consider declaring a property that is compatible with both." |
And also what you are describing here is a union of two interfaces, which conflict with each other. Because it is option in For example, how would you suggest this be resolved? interface A {
member1: string;
}
interface B {
member1: number;
}
interface C extends A, B {
} |
@kitsonk it is more like an intersection than a union... The intersection of the types would have member1 as non-optional. However since a non-optional member can still be |
intersection types are now available in
|
@mhegazy Intersection types are great and would be a workaround for the MCV as given but unfortunately don't solve the underlying problem. If you have two interfaces you don't control and need to extend both of them you cannot. (The workaround would be to declare all the methods by hand and rely on structural typing to recognise the second interface, but you lose some of the benefit of the compiler checking you have implemented it correctly). Maybe this should be a new issue, but: It seems like the rules for a) implementing multiple interfaces and b) intersection types should be consistent. Example 1: Optional members
Example 2: Protected members
Implementing two or more interfaces should create the same type as the intersection of the types. That they don't seems like a bug. In pseudocode, this:
Should be identical to this (if it was a thing - is this in 1.6? It's not in typescript@next as of 1.6.0-dev.20150811):
|
@benliddicott this seems like a reasonable idea, can you make a separate issue for it? |
I've posted a workaround for this on #4278. |
I have 3 interfaces:
I'm getting
TS2320: [Object Object]
error,because interface A's
member1
is required property, and B'smember1
is optional.How to resolve it?
The text was updated successfully, but these errors were encountered: