Detect renamed base members
Refactoring class hierarchies
TypeScript · Const
override marks a derived-class member as an intentional replacement for an inherited member. TypeScript verifies that the base class contains a matching member, while noImplicitOverride can require the annotation across a codebase.
The override modifier marks a class member as an intentional replacement for a member inherited from a base class. TypeScript checks that a corresponding member exists in the base class, and noImplicitOverride can require this modifier for every overriding member.
class Derived extends Base {
override member() {}
}The overridden member's declared return type; override itself produces no runtime value.
override modifier is invalid when the base class has no member with the same name.noImplicitOverride, omitting override from an overriding member is an error.class Album {
setup() {
return "base setup";
}
}
class SharedAlbum extends Album {
override setup() {
return "shared setup";
}
}
console.log(new SharedAlbum().setup());The derived method explicitly declares that it replaces the base method.
Refactoring class hierarchies
Large-team development
Strict compiler configurations
override modifier do?override documents and verifies that a derived-class member replaces a member from its base class. TypeScript reports an error if no same-named member exists in the base class.
Follow-up: How can a codebase require the modifier consistently? Enable noImplicitOverride; then every superclass override must explicitly use override.
override useful during refactoring?The modifier protects against accidental divergence when a base member is renamed. If the derived member is not updated, TypeScript can detect that the claimed override no longer matches.
Follow-up: What configuration complements the modifier? noImplicitOverride turns implicit overrides into errors, making the intent explicit throughout the hierarchy.
override introduced, and what option is associated with it?TypeScript 4.3 introduced override together with the noImplicitOverride option. The modifier applies to members such as methods that are intended to replace inherited members.
Follow-up: What happens if a derived method is merely intended to be new? Leave off override; adding it would cause an error unless the base class declares a matching member.
noImplicitOverrideextendssuper