Access modifiers

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[] ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.