Inheritance and Abstract Classes
Use inheritance and abstract classes to write scalable, contract-driven TypeScript code that enforces structure without sacrificing flexibility.
Class hierarchies let us model shared structure across types. In JavaScript, inheritance is flexible, but the runtime does not enforce class contracts or field types. That means mistakes like missing properties or incompatible overrides may only show up at runtime. TypeScript brings discipline to this model. It tightens the rules and clarifies the design without slowing us down.
In this lesson, we’ll unlock TypeScript’s inheritance tools: extending classes, enforcing method signatures, and defining abstract contracts that subclasses are required to complete.
Let’s make our classes do more.
Extending classes with extends
When we extend a class, we reuse its fields and methods and specialize them when needed. TypeScript checks that subclassing aligns with the base class structure at compile time.
In the following example, we’ll define a base class Employee with a field and method, then extend it with a subclass Manager that:
Adds a new field,
department.Calls the base constructor using
super().Overrides the
describe()method with more specific behavior.
This example shows how TypeScript lets us extend structure while keeping type safety fully intact while maintaining compile-time type safety. Let’s look at the code.