TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Different access modifier for getter and setter

Open oskrabanek opened this issue 11 years ago • 46 comments

Could it be possible to implement different access modifier for getter and setter? This way the setter could be for example private/protected and the getter public. In some cases this in really useful when the value shall be read-only.

Example:

class MyClass {
    private _myProp: any;
    private set myProp(value: any) {
        this._myProp = value;
    }
    public get myProp(): any {
        return this._myProp;
    }
}

oskrabanek avatar Apr 21 '15 09:04 oskrabanek

There's a Playground build for PR 42425 available for trying out and I'd appreciate any feedback on how it meets folks' needs here. Thanks! See comments in #42425 on how this works.

RyanCavanaugh avatar Mar 25 '21 03:03 RyanCavanaugh

This is nice. I hope it's a step toward asymmetric accessibility of data properties.

snarfblam avatar Mar 26 '21 22:03 snarfblam

This is great to see. Apologies my prior comment was directed to the disnissive way this issue was summarily closed by the former team manager years ago. @RyanCavanaugh I'm glad to see typescript gaining more parity with languages like C#. Great work.

legistek avatar Mar 26 '21 22:03 legistek

The recommendation here is to use a public readonly getter and a private/protected backing field.

Accessors are symetrical with properties in the type system. anything we do will need to be manifested in the type and expressible on properties. Adding new access modifiers to enable private_set/public_get would increase the complexity of the language and the learning curve, and the value gained from this would not match the added complexity.

I am sorry but this is a rubbish reason. Typescript includes features like private constructors, yet you say that the complexity of different getters/setters is too high and steepens the learning curve. Like somebody mentioned here, it is hard to make should not be an argument against a standard in most modern programming languages. Your suggestion to use private/protected is not really helpful either - it makes the code less readable and it adds complexity to it.

codearmadillo avatar Sep 14 '21 06:09 codearmadillo

I am sorry but this is a rubbish reason. Typescript includes features like private constructors, yet you say that the complexity of different getters/setters is too high and steepens the learning curve. Like somebody mentioned here, it is hard to make should not be an argument against a standard in most modern programming languages. Your suggestion to use private/protected is not really helpful either - it makes the code less readable and it adds complexity to it.

You might want to consider reading more than the first few comments. The one you quoted was written over 5 years ago, several months before even the addition of private constructors (TypeScript 2.0) you're talking about. TypeScript has gone a long way since then, see the comments and linked commit (which landed in 4.3) right above.

RiftLurker avatar Sep 14 '21 07:09 RiftLurker

@RiftLurker I know that, but that does not change the point. I did read the thread, and I do know the changes are in 4.3.

codearmadillo avatar Sep 14 '21 07:09 codearmadillo