Similar to .NET, TypeScript allows you to define access modifiers to class members. This is a great feature as it expands your ability to author well-designed, encapsulated, and safer code. In JavaScript, for example, a common convention to represent private and internal code is to use an underscore (_) prefix, which should no longer be needed.
TypeScript classes support multiple access modifiers – private, protected, and public (default):
- Private: Members are accessible from within the instance of the same class
- Public: Members are accessible everywhere
- Protected: Members are accessible from within the instance of the same or a derived class
Consider the following example:
class SumCalculator { protected history: Calculable[] ...