Private setter typescript?

前端 未结 2 959
盖世英雄少女心
盖世英雄少女心 2020-12-30 18:32

Is there a way to have a private setter for a property in TypeScript?

class Test
{
    private _prop: string;
    public get prop() : string
    {
        re         


        
2条回答
  •  执笔经年
    2020-12-30 19:03

    I also hope we could have public getter and private setter. Until we do, another way to handle this is to add additional private getter and setter:

    class Test {
      _prop: string;
      public get prop(): string {
        return this._prop;
      }
    
      private get internalProp(): string {
        return this.prop;
      }
    
      private set internalProp(value: string) {
        this._prop = value;
      }
    
      private addToProp(valueToAdd: string): void {
        this.internalProp += valueToAdd;
      }
    }
    

提交回复
热议问题