-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TS Proposal : "Interface incorrectly extends interface" - sub-interface method overload OR override ? #20920
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
This issue is also applicable for enums that are overridden. interface BaseType
{
allowedSounds: 'BARK' | 'MOO';
}
interface DerivedType extends BaseType
{
allowedSounds: 'BARK' | 'MOO' | 'CAW';
} This currently results in the following error: Interface 'DerivedType' incorrectly extends interface 'BaseType'.
Types of property 'allowedSounds' are incompatible.
Type '"BARK" | "MOO" | "CAW"' is not assignable to type '"BARK" | "MOO"'.
Type '"CAW"' is not assignable to type '"BARK" | "MOO"'. But from a language point of view, it is a totally legit expectation to be able to override enums in DerivedTypes. |
This is a correct error. |
@RyanCavanaugh Actually on second thought, I agree with you. I myself was feeling conflicted about it, but wrote that out anyway. |
I now think it worth to add that by no mean, all through an interface inheritance chain should a method be completely wiped off (deleted, removed)... Not even by The mechanism is to ensure that only some methods mood (signatures) are handled (accepted) at a given inheritance node. In other words, if |
As I think over it... I realize that this will conflict with some end-user decorator named
|
I've got some react props along the lines of: It's really annoying having to use three different names for the
As a proposal: Remove it from typscript compiler entirely and just make it a linting error
or if this seems too crazy, these might work:
or
anything really, as long as typescript isn't blocking this js functionality. |
type ProtoExntends<T, U> = U & {
[P in Exclude<keyof T, keyof U>]: T[P];
}; Well, I found that there was an easier way used the builtin types: type ProtoExntends<T, U> = U & Omit<T, keyof U>; And for the example usage: interface Parent {
aaa: number;
bbb: number;
// .
input(stream: {}): void;
allowedSounds: 'BARK' | 'MOO';
}
interface ChildExtends {
aaa: string;
ccc: string;
// .
input(stream: {}, coordinates: {x: number; y: number}): void;
allowedSounds: 'BARK' | 'MOO' | 'CAW';
}
type Child = ProtoExntends<Parent, ChildExtends>; |
TypeScript Version : 2.6.2
The problem
Initial scenario
I encountered the issue yesterday days as I was trying to add some features to one of my interface inheritance chain. The context of this example is just imagined : It is longer than needed to demonstrate the issue, but I wanted a full example to clearly show it needs to be addressed.
So, let's imagine a testing app that should instrument devices : the following is a potential model of the situation :
Expected behavior :
It should just overload that method in the sub-interfaces.
Actual behavior :
It requires me to fully copy paste all method signatures in each interface.
When you're can have up to (why not) 15 overloads distributed through inheritance chain... It becomes, (yes, you said it) bulky.
Now it is, that the compiler cannot yet figure out when to
override
the method signatures inherited from parents and when tooverload
them.The Proposal
My first thought was to introduce a new keyword just like
@ts-nocheck
and like.@override
OR
Then, the
TouchScreenDeviceInterfaceLike
interface would become :Furthermore,
@overload
While the previous
@override
is useful when the default behavior is oveloading, we can still define the default behavior to suppress all parent definition WHEN A METHOD IS REDEFINED in subinterface, except when@overload
is used. i.eOR
And
TouchScreenDeviceInterfaceLike
interface can become :By thinking rigorously, I think the first step would be allow inheriting type to overload methods by default.
= = = = = = UPDATE = = = = = =
I now think it worth to add that by no mean, all through an interface inheritance chain should a method be completely wiped off (deleted, removed)... Not even by
@Override
! As this may (will) break the core concept of OOP inheritance.The mechanism is to ensure that only some methods mood (signatures) are handled (accepted) at a given inheritance node.
In other words, if
@Override
suppresses all signatures, at least one method signature MUST be required.The text was updated successfully, but these errors were encountered: