Access Modifiers
Access modifiers (public, protected, private, plus readonly) restrict who may read or write a class member in the type system. public is the default. Only # fields are private at runtime.
Detailed Explanation
A class member without a modifier is public. Anyone who has the instance may read and call it. private limits access to the body of the declaring class. protected allows the declaring class and its subclasses. These checks are compile-time only. The emitted JavaScript still has the properties on the instance, named as you wrote them, and any JS file can read them.
That last sentence is the one that surprises people coming from Java or C#. TypeScript private is a visibility rule for the checker, not an access-control mechanism for the engine. The runtime-private form is the JavaScript #field syntax, covered under #private vs private.
readonly is a modifier that composes with the others: private readonly id: string. It prevents assignment after the constructor, still only in the type system.
Parameter properties put a modifier on a constructor parameter and declare the field in one step: constructor(private readonly id: string) {}. They are a visibility feature and a declaration feature at once.
On types, private and protected members change assignability. Two otherwise identical classes are not assignable if each has its own private id. A subclass may assign to a protected member; foreign code may not. A public API that mentions a class with private fields leaks those fields into the structural contract — consumers cannot forge the type without going through the constructor.
Interfaces cannot have private or protected members. If you need to hide a field from the public type, keep it on the class (or use #) and expose an interface that omits it. implements checks the public shape only.
Do not use private for security. Do not use it to hide secrets in a browser bundle; they are still in the source. Use it to stop other TypeScript code from depending on a field you want to rename, and accept that JavaScript callers are unconstrained.
Code Examples
1. The three visibilities.
// 1. The three visibilities.
class Account {
public email: string;
protected createdAt: Date;
private passwordHash: string;
constructor(email: string, passwordHash: string) {
this.email = email;
this.createdAt = new Date();
this.passwordHash = passwordHash;
}
check(hash: string): boolean {
return this.passwordHash === hash;
}
}
const a = new Account("ada@example.com", "x");
console.log(a.email);
// console.log(a.passwordHash);
// Error: Property 'passwordHash' is private and only accessible within class 'Account'.2. protected is visible in subclasses.
// 2. protected is visible in subclasses.
class StaffAccount extends Account {
daysOld(): number {
const ms = Date.now() - this.createdAt.getTime();
return Math.floor(ms / 86_400_000);
}
}3. private makes class types nominal-ish.
// 3. private makes class types nominal-ish.
class A { private id = "a"; }
class B { private id = "b"; }
// const x: A = new B();
// Error: types have separate private 'id' declarations.Common Gotchas
`private` is erased
`account["passwordHash"]` and any plain JS can still read the field. Use `#passwordHash` if runtime privacy matters.
Interfaces cannot declare `private` members
put hidden state on the class, and export a public type that does not mention it.
Default is `public`
writing `public` is documentation. Teams differ on whether to require it; pick one style.
Exporting a class exports its private field names in the type
a consumer's structural assignability has to mention those privates. Prefer exporting an interface if the class is only an implementation.