-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Reject invalid overriding of class methods with interface arguments #45854
Comments
The suggestion to do something different is part of #18770. I’m going to call this a duplicate rather than “Working as Intended” since I don’t want to imply that we would never be open to tightening this up. FWIW, I feel like the FAQ entry isn't particularly satisfying in the years since |
Hi @MartinJohns and @andrewbranch Thank you for your prompt responses! Actually I was expecting to have an error like this one: interface IA {
a: string
}
interface IB extends IA {
b: string
}
function doB(i: IB) {}
// Type '(i: IB) => void' is not assignable to type '(i: IA) => void'.
// Types of parameters 'i' and 'i' are incompatible.
// Property 'b' is missing in type 'IA' but required in type 'IB'.(2322)
const doA: (i: IA) => void = doB But I get your point. Thanks |
It's mentioned in the PR that implemented the flag. See #18654, specifically:
Which is also mentioned in the release notes for TS 2.6: |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
The following code does compile (tested with TypeScript 4.4.2), but I would except it to not compile:
The execution result of this code is:
Hence,
B::do(i: IB)
method can be called with anIA
argument, which means thati.b
can beundefined
.The suggestion is to add a compilation error on
B::do(i: IB)
prototype.I've hesitated to submit this as a bug since compiler does report an error for the same kind of design error using arguments such as
string
. For example, the compiler correctly reject the following code:🔍 Search Terms
class method override interface argument
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
I would suggest to make the compiler to check that in the overriding prototype, interface arguments do not require anything more than what the overridden argument initially provides.
📃 Motivating Example
The code provided as illustration is a motivation example: since the compiler does not report any error, a programmer would consider he will always deal with an
IB
argument with a definedb
property inB:do(i: IB)
while it actually can beundefined
since the method can be called with anIA
argument. In such a situation, unexpected behavior/bugs are possible at run time.💻 Use Cases
The current workaround is human code review to avoid such programming design error.
The text was updated successfully, but these errors were encountered: