Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private setter typescript?

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

class Test {     private _prop: string;     public get prop() : string     {         return this._prop;     }      private set prop(val: string)     {         //can put breakpoints here         this._prop = val;     } } 

Compiler complains that visibility for getter and setter don't match. I know I can just set the backing field, but but then I can't set breakpoints when the value is set.

I though about using an interface to hide the setter, but interfaces can only define a property, not whether it has a getter on setter.

Am I missing something here? There doesn't seem to be any reason to not allow private setters, the resulting JS doesn't enforce visibility anyway, and seems better that the current alternatives.

Am I missing something? If not is there a good reason for no private setters?

like image 546
sheamus Avatar asked Jan 07 '15 17:01

sheamus


People also ask

Can we use getter setter in TS?

In TypeScript, there are two supported methods getter and setter to access and set the class members.

Can getter setter be private?

The private getter/setter methods provide a place for adding extra behavior or error checking code. They can provide a place for logging state changes or access to the fields. They can provide a place for adding your debug code while testing.

What is a private setter?

private setters are same as read-only fields. They can only be set in constructor. If you try to set from outside you get compile time error. public class MyClass { public MyClass() { // Set the private property.

What are accessors in TypeScript?

The TypeScript accessor provides us with a way to implement encapsulation. In simple terms, encapsulation hides the data members so that they can only be accessed by the member functions of the same class. The code snippet below provides an example of encapsulation .


1 Answers

The TypeScript specification (8.4.3) says...

Accessors for the same member name must specify the same accessibility

So you have to choose a suitable alternative. Here are two options for you:

You can just not have a setter, which means only the Test class is able to set the property. You can place a breakpoint on the line this._prop =....

class Test {     private _prop: string;     public get prop() : string     {         return this._prop;     }      doSomething() {         this._prop = 'I can set it!';     } }  var test = new Test();  test._prop = 'I cannot!'; 

Probably the ideal way to ensure private access results in something akin to a "notify property changed" pattern can be implemented is to have a pair of private get/set property accessors, and a separate public get property accessor.

You still need to be cautious about someone later adding a direct call to the backing field. You could get creative in that area to try and make it less likely.

class Test {     private _nameBackingField: string;      private get _name() : string     {         return this._nameBackingField;     }      private set _name(val: string)     {         this._nameBackingField = val;         // other actions... notify the property has changed etc     }      public get name(): string {         return this._name;     }      doSomething() {         this._name += 'Additional Stuff';     } } 
like image 191
Fenton Avatar answered Sep 19 '22 17:09

Fenton