public / private / protected
public, private, and protected are the three visibility keywords on class members. public is open, private is the declaring class only, protected is the class and its subclasses. All three are type-system checks.
Detailed Explanation
These three keywords are the vocabulary of class visibility. They apply to fields, methods, accessors, and constructor parameter properties. They do not apply to interfaces, type aliases, or object literals.
public means any TypeScript that can see the instance can use the member. It is the default, so writing it is optional. Public members are the class's contract. Changing a public name is a breaking change for every importer.
private means only the body of this class declaration can use the member. Subclasses cannot. Other files cannot. A method on the same class can read another instance's private field (equals(other: this) { return this.id === other.id; }), because privacy is per class, not per instance. That matches Java and C# and surprises people who expected instance isolation.
protected means this class and classes that extend it. A subclass method may read this.createdAt if the base declared it protected. Code that merely has an instance of the subclass still cannot write instance.createdAt. Protected is for "implementation inheritance" — shared guts — not for a public API.
Visibility is not inheritance of types the other way. A subclass may widen access (protected in the base, public in the child) in some cases and may not narrow it (public in the base, private in the child is an error). The public contract of the base has to remain callable on the child.
None of these keywords emit as JavaScript visibility. The members are ordinary properties. The exception is a #name field, which is a fourth, runtime mechanism and does not use these keywords.
When you document a class, list the public members and pretend the rest do not exist. When you choose among the three, start at private and open up. protected is a promise that subclasses are a supported extension point; if you do not have subclasses, private is enough.
Code Examples
1. Who can read what.
// 1. Who can read what.
class Job {
public readonly id: string;
protected status: "queued" | "running" | "done";
private workerId: string | undefined;
constructor(id: string) {
this.id = id;
this.status = "queued";
}
start(workerId: string): void {
this.workerId = workerId;
this.status = "running";
}
}
class PrintJob extends Job {
label(): string {
return `${this.id}:${this.status}`;
// return this.workerId;
// Error: 'workerId' is private in Job.
}
}
const job = new PrintJob("j1");
console.log(job.id);
// console.log(job.status);
// Error: 'status' is protected.2. Privacy is per class, not per instance.
// 2. Privacy is per class, not per instance.
class Box {
private value: number;
constructor(value: number) {
this.value = value;
}
eq(other: Box): boolean {
return this.value === other.value; // allowed — same class
}
}3. You cannot narrow a public member in a subclass.
// 3. You cannot narrow a public member in a subclass.
class Base {
public name(): string {
return "base";
}
}
class Child extends Base {
// private name(): string { return "child"; }
// Error: 'name' is public in Base and cannot be private in Child.
public name(): string {
return "child";
}
}Common Gotchas
`private` is still present on the object at runtime
`JSON.stringify` and spread will include it. Use `#` or omit the field from a DTO mapper.
Cross-class `private` members with the same name are not compatible
this is why two classes with `private id` are not structurally assignable.
`protected` is an extension-point promise
if you never wrote a subclass, `private` documents the intent better.
Parameter properties inherit these rules
`constructor(private id: string)` is a private field, not a public one. Forgetting the modifier makes it public.