-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Override keyword should not be required on optional base type members, without implementations. #44439
Comments
(Crossposting this comment) This is intentional. The mental model to have in place here is, "If I typo this method name, what will happen?" If you typo the name of an abstract member (assuming you're in a concrete class), you'll get an error, because you failed to implement all abstract members. If you typo the name of an optional member, you'll now have |
@RyanCavanaugh thanks for the reply. I definitely understand the design goal of explicit declaration here. The challenge I'm seeing is the mismatch between class members that are optional, and interface methods that are optional. It appears interfaces are not held to the same standard as classes, and abstract class members are treated differently than non-abstract. Below will actually throw a compilation error, as override is not supported on interface methods. I'm not sure if this is a gap, but it seems inconsistent from the perspective of abstract classes and interfaces achieving similar goals. interface BaseType {
customInit?(): Promise<void>;
}
class Specialization implements BaseType {
override async customInit() {
}
} |
It's intentional that |
Right, but I'd argue that an optional member on an abstract class is not being overridden either, since it does not have an implementation. The scenario you provided with interface Calculatable {
calculateHeight?(): number;
}
class Calculator implements Calculatable {
/* overide*/ cacluateHeight() {
return 0;
}
} vs abstract class BaseCalculator {
calculateHeight?(): number;
}
class Calculator extends BaseCalculator {
overide cacluateHeight() {
return 0;
}
} |
@RyanCavanaugh From conversation it appears the goal of I still believe this desire/need can apply to interfaces as well, though the current issue has a work around. By declaring the optional method via an interface, the compiler will no longer consider the method a candidate for overriding, and so will not complain. It feels a little clunky, especially given abstract classes cannot implement interfaces with optional members. interface OptionalBaseTypeMethods {
customInit?(): Promise<void>;
}
abstract class BaseType {
abstract requiredSettings(): object;
}
const BaseTypeClass = BaseType as (typeof BaseType & OptionalBaseTypeMethods);
class Specialization extends BaseTypeClass {
requiredSettings() {
return {};
}
async customInit() {
}
} |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
override keyword optional
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
When using the
noImplicitOverride
flag, an optional base type field, with no implementation, requires theoverride
keyword. By contrastabstract
methods do not require theoverride
keyword.π Expected behavior
An optional field, with no implementation in the base class should not require the override keyword.
The text was updated successfully, but these errors were encountered: