Different modifiers in class of TypeScript

Different modifiers in class of TypeScript

Different modifiers in class of TypeScript

In TypeScript, we can use different modifiers to decorate the properoty in a class, and there are some differences in each mdifiersand there are some differences between each other:

  • public: The default modifier of property if you don't set it explictly. The property decorated by public can be visited and modified in the origin class, instances and the subclass
  • private: The properties which are decorated by private modifier, they cannot be accessed from outside of its containing class and its instance, just like:
class A {
     private name: string = 'private name'
 }

 class B extends A {
     show() {
         // error: Property 'name' is private and only accessible within class 'A'.
         console.log(this.name)
     }
 }

 const a = new A()
 a.name // error: can't access 'name' in the instance of class, only in class
  • protected: It is similar with private modifier, but the property which is decorated by it, can be visited in its own class and its subclass, but can't be visited in the instances, just like:
class A {
       protected name: string = 'protected name'
   }

   class B extends A {
       show() {
           console.log(this.name)
       }
   }

   const a = new A()
   a.show() // console 'protected name'
   a.name // error: Property 'name' is protected and only accessible within class 'A' and its subclasses.
  • readonly: can't be modified, only read
  • static:
    1. can't be visited in instance
    2. cannot be accessed from outside of its containing class
    3. can be visited by ClassName.propertyName:
class A {
    static hi: string = "hello"
}

A.hi // show: hello
  • abstact: An absctarct property must be defined in an abstract class, and can't make an instance from an absctract class. The property must be implmented in derived classes.
abstract class Department {
  constructor(public name: string) {}

  printName(): void {
    console.log("Department name: " + this.name);
  }

  // only declare but dont implement it, must be implemented in derived classes
  abstract printMeeting(): void;
}

class AccountingDepartment extends Department {
  constructor() {
    super("Accounting and Auditing"); // constructors in derived classes must call super()
  }

  printMeeting(): void {
    console.log("The Accounting Department meets each Monday at 10am.");
  }

  generateReports(): void {
    console.log("Generating accounting reports...");
  }
}
let department: Department; // ok to create a reference to an abstract type
department = new Department(); // error: cannot create an instance of an abstract class
department = new AccountingDepartment(); // ok to create and assign a non-abstract subclass
department.printName();
department.printMeeting();
department.generateReports(); // error: method doesn't exist on declared abstract type
const newDep: AccountingDepartment = new AccountingDepartment();
newDep.generateReports(); // It's ok, generateReports exist in class AccountingDepartment