override modifier to enforce strict compiler checks during this inheritance mechanism.
Syntax and the override Modifier
The override keyword is placed immediately before the method name in the subclass. It explicitly signals to the compiler that the method is intended to replace a base class implementation.
Crucially, the override keyword inherently instructs the compiler to verify that a method with the exact name exists in the base class. If the base method does not exist (e.g., due to a typo or a refactor in the base class), the compiler throws an error regardless of any other compiler configurations.
Compiler Configuration: noImplicitOverride
TypeScript provides the noImplicitOverride compiler option in tsconfig.json. When set to true, the compiler strictly enforces the presence of the override keyword. If a subclass method shares a name with a base class method but lacks the override modifier, the compiler throws an error, preventing accidental shadowing.
Signature Compatibility Rules
TypeScript enforces structural typing rules when a method is overridden. 1. Parameter Bivariance Unlike strict Liskov Substitution Principle (LSP) enforcement which requires contravariance, standard method parameters in TypeScript are bivariant. This remains true even when thestrictFunctionTypes compiler option is enabled. Consequently, an overriding method is permitted by the compiler to accept narrower types (subtypes) or wider types (supertypes) than the base class method. It cannot require new mandatory parameters, but it can add new optional parameters.
protected method can be overridden as public, but a public method cannot be overridden as protected or private.
Base Method Invocation
When a method is overridden, the subclass implementation entirely shadows the base class implementation. To execute the original base class logic within the overriding method, thesuper keyword is used to reference the superclass prototype.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More





