0

From https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#1.6

The 'public' keyword denotes that the constructor parameter is to be retained as a field. Public is the default accessibility for class members, but a programmer can also specify private or protected accessibility for a class member. Accessibility is a design-time construct; it is enforced during static type checking but does not imply any runtime enforcement.

I wonder why it couldn't be enforced during runtime... Or perhaps it's intentionally designed this way?

fatdragon
  • 2,211
  • 4
  • 26
  • 43
  • 1
    Possible duplicate of [TypeScript private members](http://stackoverflow.com/questions/12713659/typescript-private-members) – Nitzan Tomer Sep 14 '16 at 07:20
  • Just like with type-checking, private is only enforced in compilation time but isn't present at runtime. Typescript is a superset of javascript, meaning that it has features that js does not, when compiled into js all of those features are removed to make it compatible. – Nitzan Tomer Sep 14 '16 at 07:23
  • I agree. Thanks for the insight! – fatdragon Sep 14 '16 at 07:46

1 Answers1

-1

Accessibility is a design-time construct; it is enforced during static type checking but does not imply any runtime enforcement.

This is covered in the FAQ https://github.com/Microsoft/TypeScript/wiki/FAQ#you-should-emit-classes-like-this-so-they-have-real-private-members

basarat
  • 261,912
  • 58
  • 460
  • 511
  • I'm afraid that I don't follow... Are you saying that it's impossible to implement a truly private member in TypeScript because of its compilation into javascript? In that case, it's an unfortunate choice rather than being designed on purpose. – fatdragon Sep 14 '16 at 07:05