Method overriding in TypeScript occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The subclass method must share the same identifier and possess a type-compatible signature with the base class method. TypeScript 4.3 introduced the explicitDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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.
Master TypeScript with Deep Grasping Methodology!Learn More





