Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TypeScript prohibit a private setter for a public getter of the same name?

Tags:

typescript

Having a public getter and a private setter of the same name is prohibited in TypeScript, as already discussed in various questions here on SO.

My main programming language is C#, where this is perfectly fine. I also consider it a useful construct, and to be honest, I don't really see what a problem could be with allowing this in TypeScript as well.

Why does TypeScript prohibit this? This question is not about workarounds or alike, I just want to know the reason for this design.

Edit: A code sample can be found on this question.

like image 466
theDmi Avatar asked Feb 16 '16 09:02

theDmi


1 Answers

There is a comment from one of TypeScript authors: https://github.com/Microsoft/TypeScript/issues/2845#issuecomment-176990923

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.

like image 135
Martin Vseticka Avatar answered May 21 '23 04:05

Martin Vseticka