Inheritance
A class inherits with extends. The subclass gets the parent's instance members, may override methods, and must call super() if it has its own constructor. Types flow the same way: a subclass instance is assignable to the parent type.
Detailed Explanation
TypeScript inheritance is JavaScript inheritance plus types. class Admin extends User sets up the prototype chain at runtime. Admin instances have User methods. instanceof User is true. The compiler also treats Admin as a subtype of User for assignability, as long as the subclass does not violate the parent's public contract.
The subclass constructor must call super(...) before it uses this, same as JavaScript. Parameter properties on the subclass are initialized after super. If the parent constructor requires arguments, the child has to pass them.
Overriding a method keeps or narrows the return type and keeps parameter types compatible. The override keyword (and the noImplicitOverride flag) makes missed renames a compile error: if the parent no longer has that method, override fails. Without the keyword, a typo creates a new method and the parent one stays on the prototype, unused.
Fields override fields. A subclass may redeclare a field with an initializer; the exact emit order (parent fields, parent constructor body, child fields, child constructor body) is the JavaScript order and a common source of "why is this undefined in super()" bugs. Do not read subclass fields from the parent constructor.
TypeScript models the subtype relationship structurally and via the heritage clause. Private members on the parent are part of the nominal check: you cannot declare an unrelated class with the same public shape and the same private name and pass it off as the parent. Public-only shapes still assign structurally.
Prefer composition when the shared behavior is a capability (Logger, Clock) rather than an "is-a" relationship. Prefer an interface when you only need a contract. Use extends when you are actually specializing a class and want the prototype chain.
Multiple inheritance of classes is not allowed. Mixins are a pattern on top of extends and intersections, not a language feature. A class may still implement many interfaces.
Code Examples
1. A subclass, super, and assignability.
// 1. A subclass, super, and assignability.
class User {
constructor(public email: string) {}
domain(): string {
return this.email.split("@")[1] ?? "";
}
}
class Admin extends User {
constructor(email: string, public role: "admin") {
super(email);
}
}
const user: User = new Admin("ada@example.com", "admin");
console.log(user.domain()); // example.com2. override keeps the rename honest.
// 2. override keeps the rename honest.
class Job {
label(): string {
return "job";
}
}
class PrintJob extends Job {
override label(): string {
return "print-job";
}
}3. Parent constructors run before child fields.
// 3. Parent constructors run before child fields.
class Base {
constructor() {
this.setup();
}
setup(): void {}
}
class Child extends Base {
ready = true;
override setup(): void {
// this.ready is undefined here — the child field has not been set yet
console.log(this.ready);
}
}
new Child(); // logs undefinedCommon Gotchas
`super()` is required before `this` in a derived constructor
the compiler and the engine both enforce this.
Parent constructors that call overridable methods see uninitialized child fields
do not call `this.virtual()` from a base constructor.
`noImplicitOverride` makes forgotten `override` an error
turn it on if you rely on inheritance. A typo without `override` silently adds a new method.
Changing a parent method's parameters is a breaking change for every override
the child signatures must stay compatible.