An abstract class in TypeScript is a restricted class declared with theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
abstract modifier that serves exclusively as a base class. It cannot be instantiated directly using the new keyword. Instead, it establishes a strict structural contract for derived classes by combining fully implemented members with abstract members (properties and methods) that lack implementation.
Syntax and Structure
Abstract classes are defined using theabstract keyword before the class declaration. Members within the class that require implementation by subclasses are also prefixed with abstract.
Core Mechanics and Rules
- Instantiation Restriction: The TypeScript compiler will throw an error if you attempt to instantiate an abstract class directly (
new AbstractBase()). - Implementation Mandate: Any concrete (non-abstract) class that extends an abstract class must implement all inherited
abstractproperties and methods. If a derived class fails to implement even one abstract member, it must also be declared asabstract. - Method Signatures and Type Variance: Abstract methods only define the call signature (parameters and return type). They are terminated with a semicolon instead of a block
{}. When implementing these methods, class methods defined using themethodName(arg: Type): voidsyntax are bivariant in TypeScript. This means a derived class can implement an abstract method with a narrower or wider parameter type without triggering a compiler error. - Access Modifiers:
- Abstract members can be
publicorprotected. - Abstract members cannot be
private, as derived classes would be unable to access and implement them. - Abstract members cannot be
static.
- Abstract members can be
- Constructors: Abstract classes can define constructors. A derived class is only required to explicitly call
super()if it defines its ownconstructorblock. If the derived class omits the constructor entirely, TypeScript automatically calls the base class constructor upon instantiation, inheriting the base constructor’s parameter signature.
Implementation Example
To safely handle varying data types in derived classes, abstract classes frequently utilize generic type parameters.Compilation Behavior
Unlike TypeScriptinterface declarations, which are completely erased during compilation, abstract classes are emitted into the final JavaScript code as standard ES6 classes. The abstract keywords are stripped away, but the concrete methods, properties, and constructors remain intact in the compiled output, allowing them to be utilized at runtime via prototype chain inheritance.
Master TypeScript with Deep Grasping Methodology!Learn More





