What are access modifiers in TypeScript?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid TypeScript basics — a prerequisite for any developer role.
Answer
TypeScript provides three access modifiers to control the visibility of class members. public (default): the member is accessible from anywhere — inside the class, outside the class, and in subclasses. Explicitly writing public is optional but improves clarity. private: the member is only accessible inside the class where it is declared. Subclasses cannot access it. Example: class BankAccount { private balance: number = 0; }. Important: TypeScript's private is a compile-time only restriction — the property still exists and is accessible at runtime via JavaScript. For true runtime privacy, use JavaScript private fields (#balance). protected: accessible inside the class and in any class that extends it (subclasses), but not from outside the class hierarchy. Useful for template method patterns where base classes share implementation details with subclasses. Access modifiers exist only in TypeScript — they are completely erased after compilation to JavaScript.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last TypeScript project, I used this when...' immediately makes your answer more credible and memorable.