-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Implementation of interface requires members redeclaration #5749
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
Comments
Stating class B implements interface A is like promising to implement each member present in A inside B. |
This looks like a duplicate of #340. @RyanCavanaugh explained some of the rationale here: #340 (comment). |
@GoToLoop Thanks, I know what is interfaces and what is classes.
Exactly. This is why duplicate declaration of members shouldn't be needed. @DanielRosenwasser I am talking here about implementation of one interface, while @RyanCavanaugh shows examples of multiple classes. These cases are not relevant, you can handle multiple implementation in other way than since implementation. Still, I want to comment some moments:
In case of single implementation it's "incorrectly implements" if signatures are mismatched. This is why there is no rationale to duplicate them. Even more, with multiple implementations you still get that error: interface A {
foo:Function;
}
interface B {
foo:boolean;
}
class C implements A, B {
foo:boolean;
constructor() {
this.foo = true;
}
} Here "C incorrectly implements A", which is expected. Even more, @RyanCavanaugh 's examples are relevant only to methods of class which absolutely should have their own rule since overloading is different topic. One more interesting about multiple implementations and different methods signature is that there is only one way and all other ways report "incorrect implementation", this way: interface A {
foo(a:string):void
}
interface B {
foo(a:number):void
}
class C implements A, B {
foo: (a:string | number) => void;
constructor() {
this.foo = (a:string | number) => {}
}
} So again, if this is only way to handle multiple implementations and all other ways throw, then signature of |
And please, open this back. I do not want create new issue for it
|
The issues is a duplicate of #340. While the example given demonstrates why it becomes complex when implementing multiple interfaces, it is the root cause of why the team of chosen to not implement this feature as #340 states, it is too complex and they consider it out of scope. Opening another issue will just get it closed and marked as a duplicate again. Your example is that you have an interface that provides no implementation of method The way you want to deal with the code seems more in line with the |
This seems to work in a export interface A {
foobar: number;
}
export interface B extends A {
new(): B;
quux: number;
// More members go here.
}
export var C: B; C is now assumed to be a class correctly implementing interfaces A and B. Only the interface defining the actual type of C should have a class D extends C {} |
Here TypeScript says that
B
incorrectly implementsA
and requiresB
to also havefoo
property with the same signature. So question is: why I need duplicate definition of properties when implement interface if I cannot override that definition anyway:Here
B
again incorrectly implementsA
. Only way to fix it is to addfoo:Function
toB
which absolutely has no point sinceA
already has that.The text was updated successfully, but these errors were encountered: