[Typescript 4.9] Auto-Accessors in Classes

Typescript 4.9 supports an upcoming feature: Auto-accessors:

class Person {
  accessor name: string
  constructor(name: string) {
    this.name = name
  }
}

 

Under the covers, it is a "getter' and "setter" with an unreachable private property.

class Person {
   #__name: string
  constructor(name: string) {
    this.name = name
  }
    get name() {
     return this.#__name
    }

    set name(name: string) {
     this.#__name = name
    }
}

 

posted @ 2022-11-30 15:24  Zhentiw  阅读(46)  评论(0编辑  收藏  举报