-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Method overloads are not inherited from implemented interfaces #56909
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
The primary question is whether this is even intended - does it make sense to discard the class' signature in favour of ones defined in any interfaces? I can't think of situations from the top of my head where this would not be the case. |
It's intended. Your class method can deal with any |
The same argument applies to regular method overloads, class C {
fn(str: "a"): void;
fn(str: "b"): void;
fn(str: string): void {}
}
declare const c: C;
c.fn
// ^?
// Now correctly is (str: "a"): void with +1 overload
That is the one major problem I see with this too, see my previous comment are my thoughts on that. |
Because in that case you explicitly declare it as such. The public API explicit declares only those two values are supported. You don't do that in your initial example, you don't provide any overloads in your class. Perhaps it helps you to know that implemented interfaces in TypeScript are more of an assignability check, making sure your class is assignable to the interface. |
That makes sense, but doesn't quite align with the fact that JSDoc is inherited from implemented interfaces: interface I {
/**
* Does a thing
*/
fn(): void;
}
class C implements I {
fn(): void {}
}
declare const c: C;
c.fn I understand that's a different thing, but still, it displays some kind of "inheritance". |
This really is no different from asking why... interface I {
fn(str: "a"): void;
fn(str: "b"): void;
}
class C implements I {
fn(str: string): void {}
fn2(num: number): void {}
}
declare const c: C;
c.fn2(42); ...works. The contract implied by the interface is met ( Ultimately, I think this is, if not an exact duplicate, heavily related to #32082. |
The intentional design here is that writing an |
Hmm, Iβm struggling to figure out how this could be worded better but coming up empty. The hypothetical error this would cause would be in the class definition itself, and clearly adding an |
What I mean is that elsewhere if you had const x = new C();
x.fn("hello world"); then you shouldn't get a new error on |
π Search Terms
method function overload interface intheritance
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgJLIN4Chm+TEACgGcwoAuZAIjioEpKA3Ae2ABMBuHPAksyqgCN6TVpywBfLFgQAbOMWLIAwsmABbAA6yI6iOCXpsefEVIVk50AHMGyFu0xSpWNhDlwoKBMxClkCJTKXDIAdARYAPSRyAB6APxRMQDKABbMAK6ybMiCKHwWNCL2YsgA7sBgqcgA1ACMyMyM0LLMcGxAA
π» Code
π Actual behavior
Both signatures for
fn
declared inI
should be inherited byC
, and the generic implementation that takesstr: string
should not be considered the primary signature. Instead, the signaturesfn(str: "a"): void
andfn(str: "b"): void
should come from the interface.This would also make sense when implementing several interfaces with different signatures for the same function, you wouldn't only want to get the merged function in the end there.
π Expected behavior
see above.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: