Sub topic 8
Classes and Private Members
Assume you know basic TypeScript classes, interfaces, type aliases, and assignability. This chapter focuses on how TypeScript compares class instances.
TypeScript is structurally typed. For most objects, the checker compares members rather than declared names. Classes follow that rule for public members, but private and protected members add an important exception.
Public class members are compared by shape
Two classes with matching public members can be compatible even when they have different names or implementations.
class Point {
constructor(
public x: number,
public y: number
) {}
}
class Vector {
constructor(
public x: number,
public y: number
) {}
}
const point: Point = new Vector(3, 4); // OKPoint and Vector have the same public instance shape. The type checker does not require Vector to explicitly implement Point.
The reverse direction also works when the source has at least the members required by the target.
class ColoredPoint {
constructor(
public x: number,
public y: number,
public color: string
) {}
}
const point: Point = new ColoredPoint(3, 4, "red"); // OKThe target type needs only x and y. Extra public members do not prevent assignment.
| Member kind | Compared structurally? | Main rule |
|---|---|---|
| `public` | Yes | Names and types must be compatible |
| `private` | No | Declarations must come from the same class |
| `protected` | No | Declarations must come from the same class or compatible subclass chain |
| `static` | Separately | The constructor values are compared separately from instances |
Private members create nominal behavior
A private member is accessible only inside its declaring class. It also carries the identity of that declaration into assignability checks.
class Account {
private balance = 0;
deposit(amount: number): void {
this.balance += amount;
}
}
class DifferentAccount {
private balance = 0;
deposit(amount: number): void {
this.balance += amount;
}
}
const account: Account = new DifferentAccount();
// Error: each class declares its own private 'balance' memberThe two classes look identical from outside, but their balance members come from different declarations. TypeScript therefore treats their instances as incompatible.
This prevents code from pretending that a private implementation detail has the same meaning merely because it has the same name and type.
A subclass retains compatibility with its base class because the inherited private member originated in the base class.
class Account {
private balance = 0;
deposit(amount: number): void {
this.balance += amount;
}
}
class SavingsAccount extends Account {
addInterest(rate: number): void {
// The private balance is still part of the inherited identity.
this.deposit(rate);
}
}
const account: Account = new SavingsAccount(); // OKThe subclass cannot directly access balance, but its instances contain the same declared private member through inheritance.
Protected members have similar rules
A protected member is available inside the declaring class and its subclasses. For compatibility, the member must still originate from the same class hierarchy.
class Animal {
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Dog extends Animal {
speak(): string {
return `${this.name} barks`;
}
}
const animal: Animal = new Dog(); // OKA separate class with an independently declared protected name is not compatible with Animal, even if all visible members match.
Choosing an interface or a class type
An interface expresses a public capability. A class type also includes its private and protected identity.
interface HasArea {
area(): number;
}
class Circle implements HasArea {
constructor(private radius: number) {}
area(): number {
return Math.PI * this.radius ** 2;
}
}
function printArea(shape: HasArea): void {
console.log(shape.area());
}
printArea(new Circle(2)); // OKThe function depends only on area, so it accepts any suitable object. It does not unnecessarily require the object to be a particular class.
Exercise
Two classes each declare private id: string and have the same public methods. Are their instances assignable to each other?
Show answer
No. Each private id comes from a different declaration, so the classes are incompatible.