Skip to content
tezvyn:
Menu

TypeScript Class Access Modifiers

Source: typescriptlang.orgMediumHow cards are made

Access modifiers are like privacy settings for class members, controlling what code can see them. Use public (default), private (class-only), and protected (class and subclasses) to enforce encapsulation.

Why it exists

Access modifiers exist to control the visibility of class members (properties and methods). This practice, called encapsulation, prevents external code from depending on internal implementation details. By hiding internals and exposing a clear public interface, you make classes easier to refactor and maintain without breaking dependent code.

The mental model

Think of access modifiers as setting permissions on a shared document. A public member is viewable and editable by anyone. A protected member is for you and your direct team (subclasses). A private member is for your eyes only (the class itself).

How it works

TypeScript provides three access modifier keywords. If no modifier is specified, the member is public by default.

Public

Members are accessible from anywhere, both inside and outside the class. This is the default behavior.

Private

Members are only accessible from within the class in which they are declared. An instance of the class cannot access its own private members from the outside.

Protected

Members are accessible within the declaring class and by any subclasses that extend it. They are not accessible from unrelated code.

A key distinction exists between TypeScript's private keyword and JavaScript's native #private syntax. TypeScript's private is a compile-time check only; it's erased during compilation, and the property can still be accessed at runtime. In contrast, #private fields are enforced by the JavaScript runtime, providing "hard" privacy.

When to use it

Use public for the intended external API of your class—the methods and properties consumers of your class are meant to use. Use private for internal helper methods or state that should not be modified from the outside. Use protected when designing a class for inheritance, allowing subclasses to access and extend base functionality without exposing it publicly.

When not to use it

Avoid making everything public, as this creates a brittle API where any change can be a breaking change. Don't overuse protected, as it can tightly couple a base class to its subclasses, making refactoring difficult. For true runtime privacy or security, do not rely on TypeScript's private keyword; use the ECMAScript #private syntax instead.

One canonical example

A Person class has a protected name, so it can't be accessed directly. An Employee class extends Person and can access name within its own methods because it's a subclass. An instance of Employee cannot have its name property accessed from the outside.

class Person {
protected name: string;
constructor(name: string) { this.name = name; }
}
class Employee extends Person {
public getElevatorPitch() {

return My name is ${this.name}.; // OK to access 'name' here. } }

const emp = new Employee("Howard");

console.log(emp.getElevatorPitch()); // Prints "My name is Howard." // console.log(emp.name); // This would cause a compile-time error.

Interview question

Which access modifier allows a class member to be accessed by its own class and any derived classes, but not by external instances?

  • a.private
  • b.protectedCorrect
  • c.public
  • d.readonly
Why?

The "protected" modifier ensures that a member is accessible within its own class and by any classes that extend it, but remains hidden from external code. "private" members, however, are strictly confined to the declaring class itself, preventing access even by subclasses.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles