What are access modifiers in TypeScript?
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.