Sub topic 18
How private and protected members affect type compatibility
Assume you know basic TypeScript types, classes, inheritance, and the this keyword. This chapter focuses on structural typing: TypeScript usually compares the members that a value has instead of requiring it to come from a particular class.
Public members use structural typing
Two classes with the same public members are usually compatible, even when they have different names or implementations.
class Point {
constructor(public x: number, public y: number) {}
}
class Coordinate {
constructor(public x: number, public y: number) {}
}
const point: Point = new Coordinate(3, 4);
console.log(point.x); // 3Coordinate can be assigned to Point because both classes have compatible public x and y properties. TypeScript checks the shape of the value.
A plain object can also satisfy a class type when the class has only public instance members:
const origin: Point = { x: 0, y: 0 };Private members require the same declaration
A private member changes compatibility. The other type must contain a private member that originated in the same class declaration.
class User {
private token = "secret";
constructor(public name: string) {}
}
class Guest {
private token = "temporary";
constructor(public name: string) {}
}
// Error: User and Guest have separate declarations of private member 'token'.
// const user: User = new Guest();The two token properties have the same name and type, but they were declared by different classes. TypeScript treats them as unrelated private members.
This prevents code from pretending that two classes with hidden, class-specific state are interchangeable.
Protected members follow the same origin rule
A protected member is available inside its class and its subclasses, but not through ordinary outside code.
class Animal {
constructor(protected name: string) {}
}
class Dog extends Animal {
speak(): string {
return `${this.name} barks`;
}
}
const dog: Animal = new Dog("Rex"); // Allowed
console.log(dog);
// dog.name; // Error: 'name' is protectedDog is compatible with Animal because it inherits name from the same declaration. A separate class that declares its own protected name is not compatible merely because the names match.
Choosing the right design
| Member kind | Accessible from outside | Affects compatibility |
|---|---|---|
| `public` | Yes | Shape-based comparison |
| `private` | No | Same declaring class required |
| `protected` | No, except subclasses | Same declaring class or inheritance relationship required |
Use private or protected state when class identity matters or when callers should use controlled methods. Use public members when interchangeable shapes are intentional.
Exercise
Why is a subclass with an inherited protected member assignable to its base class, while an unrelated class with a protected member of the same name is not?
Show answer
The subclass inherits the protected member from the same declaration. The unrelated class declares a different protected member, so the members do not have the required common origin.