-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allowing subclasses to narrow types from the superclass breaks substitutability #42750
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
@justinfagnani pointed me to a couple related issues around the |
This is exactly method bivariance. |
I'm not experienced enough with type systems to know if you're using the phrase "method bivariance" to refer to the same thing as the "function parameter bivariance" from the FAQ section or if you're calling out that this is different because this involves a method of a class, rather than some arbitrary function. Do you think this is a valid issue or is this expected behavior? |
This is the expected behavior; the widespread prevalence of classes with methods that behave this way (e.g. the DOM event methods, classes with overly strict "compareTo" methods, etc) is why we still have function/method parameter bivariance. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I strongly recommend that this issue is re-opened and reviewed. This breaks typing of many OOP design patterns (e.g. abstract factory, etc), causing errors that can only be caught at runtime |
We're aware of what the implications of this are. See also https://github.com/microsoft/TypeScript/wiki/FAQ#this-is-closed-but-should-be-open-or-vice-versa |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
💻 Code
Also:
🙁 Actual behavior
TypeScript compiles this code with no error. A runtime error is thrown when
B.prototype.f
(orB.f
) attempts to call.charCodeAt
of123
.🙂 Expected behavior
TypeScript should fail to compile this code, with an error explaining that
B
may not overridef
with a function that narrows the type of parameterp
.The text was updated successfully, but these errors were encountered: