Different access modifier for getter and setter
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;
}
}
+1 I would love to see this reconsidered.
Very strange your rationale is "this would make things confusing" when many other languages already do this.
Since all the 👍s are not doing much I'd just spam with one more 👍 comment.
This would be syntactic sugar which helps make things more readable and quick to implement. Lots of languages have syntactic sugar including Typescript. There doesn't seem to be any real good reasoning not to do it and too complex is never a good reason not to do something, it's more of a lazy cop-out. Solving problems that are too complex is one of the reasons most people are drawn to software engineering. So take this complex problem and solve it in a nice not so complex way. Will it require refactoring and risky changes? Maybe but that is how things get done.
Still desirable. Other languages have no trouble supporting different visibility for get- and set- functionality. To add injury to insult, Typescript is made by Microsoft, just like C#, but the latter fully supports it:
public string myPropertyWithLimitedAccess { get; private set; }
Look at that. Beautifully readable, and totally nothing to complex.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties
There is little reason not to support:
private _myProperty: string;
public get myProperty(): string { return this._myProperty; }
private set myProperty(value: string) { this._myProperty = value; }
Bonus points in weird design decisions, because Typescript was primarily made for .NET developers to get more accustomed to working with the browser front-end.
+1 Typescript would definitely benefit from this functionality!
TypeScript is great even after C#, but this pointless limitation irritating me quite often. Please reopen it.
would increase the complexity of the language
Is this really this complex comparing to things like readonly [P in keyof T]: T[P]?
Bump. Everyone wants this feature. Shouldn't the TypesScript community get to decide?
would increase the complexity of the language
Is this really this complex comparing to things like
readonly [P in keyof T]: T[P]?
The complexity comes into play with the interaction of other language features. Unfortunately I can't find it, but IIRC RyanCavanaugh gave an example where this feature could allow you to set up and then violate invariants via inheritance using a class expression. Just because it's easy to write a certain declaration, that doesn't mean it'll always be easy to reason about how it affects things.
The question is which problems will a given feature solve, and which will it create. The former is easy to answer, the latter can surprisingly hard to answer. Unfortunately the TS team sometimes seems to respond with "OMG complexity" instead of illustrating the problem. (To be fair, the more time they spend answering query x for the nth time, the less time they have to develop.)
I don't 100% agree with the notion of "shouldn't the community get to decide", because if there is consensus among the experts actually developing the language, that should tell you something. But I think with something as requested as this, a thoughtful explanation from the team on what the trade-off is and why they're against it isn't too much to ask for.
And personally, I think the trade-off is 1000% worth it in this case. But if I can't be bothered to spec it out and make it work, I suppose I don't have too much right to complain.
Would this be a good time to revisit this issue?
The readonly modifier is great, but there are many cases where you want to be able to change the value inside of the class and still have everything be read-only on the outside.
I often choose not to make the property readonly because I don't like the noise that a private backing field + the properties add.
It would be great if there was some syntactic sugar to do this for you. Something like:
// Option 1: C# style
public name: string { get; private set; }
// Option 2: Swift style
private(set) name: string
// Option 3: Swift struct-style
public readonly name: string
mutating changeName(name: string) {
this.name = name
}
// Option 4: New keyword
public frozen name1: string
public readonly name2: string
I like option 2, imo it would fit well into the TypeScript language.
With option 3, you can only change readonly fields in functions that are marked as mutating
With option 4, frozen can only be set in the constructor, readonly can be set inside of this class, not by any external classes or classes inheriting from this class.
For reference, @yvbeek's ideas on more flexible modifiers is better suited for the discussion over in https://github.com/microsoft/TypeScript/issues/37487.
This issues is specifically for getters and setters, and has a super high number of upvotes! I think it would be useful to give getters and setters differing access modifiers (and we can update the existing set of modifiers over in #37487 if the TypeScript team ever decides it to be an acceptable thing to move forward with)
I don't 100% agree with the notion of "shouldn't the community get to decide", because if there is consensus among the experts actually developing the language, that should tell you something. But I think with something as requested as this, a thoughtful explanation from the team on what the trade-off is and why they're against it isn't too much to ask for.
@snarfblam Not to clog this thread with an irrelevant comment, but I think you have revealed a core principle for the way government should operate.
Not having this feature is a real pain for me and (among other things) made me switch from TS/NodeJS to something more... type safe. It's all fine and dandy, but when you're working on a complex project with a a lot of data structures (deeply nested many times) and you cannot model the data properly, you get the feeling that this is not a language "for big boys".
In my particular case, I want the property to be readonly, but modifiable from the inside... and also serialized to JSON. Too many hoops to jump through.
This feature might follow the same path as optional chaining. People will ask for this feature for years and then finally it will be added to the language, because it is practical and other languages offer the same feature.
Otherwise I hope that some implementation will become part of EcmaScript and then make its way to TypeScript.
I recently just switched to typescript and I've been loving the language. I'm really bummed out that this practical feature that exists in other languages hasn't been implemented here. Please do reconsider adding it in a future release regardless of how much complexity you think it might add to the language.
I achieved something similar to c# with getters that way:
export class I18nService {
private static ref: I18nService;
public static get instance(): I18nService {
if (!I18nService.ref) {
I18nService.ref = new I18nService();
}
return I18nService.ref;
}
}
Type errors like the following are easy to understand and are not complicated:
Property 'foo' is writable only in protected scope within class 'Blah' and its subclasses.
or
Property 'foo' is readable only in protected scope within class 'Blah' and its subclasses.
etc, and similar with private.
That's honestly not complicated.
BTW, I stumbled over this problem too, and I am using this kind of "hack" in the meantime:
// somewhere.ts
declare global {
type Writable<T> = { -readonly [P in keyof T]: T[P]; }
}
// example.ts
class Example {
public readonly prop: number;
public doSomething(n: number): void {
(this as Writable<this>).prop = n;
}
}
Technically, this could be used everywhere, but using this workaround should be restricted to code inside class methods.
BTW, I stumbled over this problem too, and I am using this kind of "hack" in the meantime:
I've played with this idea myself, but the problem is you have no way to distinguish between "public read-only" and truly read-only properties. This makes it not very suitable as a general-purpose solution.