Sub topic 16
Classes and Private Members
Assume you know basic TypeScript classes, interfaces, inheritance, and compiler errors. This chapter focuses on how class declarations affect assignability.
Public structure is still structural
TypeScript usually compares types by their members, not by their names. Two classes with the same public members can be compatible even when they come from separate declarations.
class Rectangle {
constructor(
public width: number,
public height: number,
) {}
area(): number {
return this.width * this.height;
}
}
class Box {
constructor(
public width: number,
public height: number,
) {}
area(): number {
return this.width * this.height;
}
}
const rectangle: Rectangle = new Box(3, 4); // OKRectangle and Box have compatible public shapes. The class names do not create a nominal type boundary by themselves.
Private members create a declaration boundary
A private member can be accessed only inside the class that declares it. It also affects assignability. Two classes are incompatible when they have private members with the same name unless those members originate from the same class declaration.
class Account {
private accountNumber: string;
constructor(accountNumber: string) {
this.accountNumber = accountNumber;
}
getNumber(): string {
return this.accountNumber;
}
}
class OtherAccount {
private accountNumber: string;
constructor(accountNumber: string) {
this.accountNumber = accountNumber;
}
getNumber(): string {
return this.accountNumber;
}
}
// @ts-expect-error: the private members come from different declarations
const account: Account = new OtherAccount("A-100");The members look identical, but TypeScript remembers that each private member has a different origin. This gives classes a limited form of nominal typing, where identity comes from declaration history rather than only from shape.
A subclass inherits the private member from its base class, so that member keeps the same origin.
class SavingsAccount extends Account {
constructor(accountNumber: string, public interestRate: number) {
super(accountNumber);
}
}
const savings: Account = new SavingsAccount("S-200", 0.04); // OKThe reverse assignment is not valid because an Account does not provide the subclass's interestRate property.
Protected members behave similarly
A protected member is accessible inside its declaring class and its subclasses, but not through ordinary external code. For compatibility, the protected member must also originate from the same class declaration.
class Vehicle {
protected brand = "unknown";
}
class Car extends Vehicle {
describe(): string {
return this.brand;
}
}
const vehicle: Vehicle = new Car(); // OKHow the rule works
The compiler therefore checks both the visible shape and the origin of private or protected members. This is why classes can be structural in general while still enforcing nominal-style boundaries where encapsulation requires them.
Exercise
Why is a class with the same public methods as another class still incompatible when both declare a private member with the same name?
Show answer
Each private member originates from a different class declaration, so the classes are not assignable even though their public shapes match.