162

For all of the different concepts that support access modifiers, such as fields, properties, methods and classes, which access modifiers are implied if not specified?

Sam
  • 40,644
  • 36
  • 176
  • 219
  • @basarat, it's been a long time since I've used TypeScript, so it's hard to remember how it works these days! As far as I remember, your answer looked reasonable, but I didn't feel it was authoritative enough for me to mark it as correct. Is your answer based on your experience with the language, or is it from info in the specifications? – Sam May 04 '16 at 08:52
  • based on specification https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md – basarat May 04 '16 at 23:31
  • 2
    In order to clarify this question and answer, typescript should have been specified in the question to make it clear. There is a typescript tag, but that is the only indication we aren't discussing scala, java, C#, C++, .... – absmiths Mar 27 '19 at 11:49
  • @basarat Your link is broken because they renamed the spec file. The new link that works is this: https://github.com/microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md – Wimateeka Aug 17 '22 at 17:24

2 Answers2

249

Everything in a class is public if not specified. Everything in a module is private unless export keyword is used.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • 6
    For people playing with a starter kit. Some of them ship with tslint and it can get very confusing. When you see this message `default access modifier on member/method not allowed` Check `tslint.json` to allow default or implied access modifier. – visheshd Mar 15 '16 at 01:35
  • 1
    Would it be possible to use a pre-transpile step to automatically assign `private`? – Qwerty Apr 26 '18 at 11:24
-4

I do not agree that

Everything in a class is public if not specified.

Everything is public, even if private is used. Just look at the transpiled code. Private annotated methods will be available public. Only transpiling will throw errors. Both public and private will be converted to <Object>.prototype.funcName

Jens Peters
  • 2,075
  • 1
  • 22
  • 30
  • 28
    Yes, this is a good point. However, my question was `which access modifiers are implied when not specified` rather than `can my code be accessed when an access modifier is not specified`. Access modifiers are a TypeScript concept rather than a JavaScript concept. So I think @basarat's answer addressed what I meant in the question and appears to be correct in context of the question. Perhaps there's some room to disambiguate the wording in the question and answer. – Sam May 25 '16 at 22:05
  • That's just how TS works, it will compile your code no matter if it's "correct" from a TS standpoint, this implies to everything in TS with no exception to classes, but the main point is if you respect TS warnings - you won't be accessing properties marked as private. – Andrew Bogdanov Sep 07 '17 at 11:41
  • 7
    I can't think of any language where something is actually private. In C# you can _easily_ get at any private field through reflection. In lower-level languages like C or C++, you can get at private fields just by moving pointers around. Short of the safeguards at the OS-level, like DEP and encrypted memory, access modifiers are just compiler-provided assistance to help you write better code. ...Right? – pbristow Jun 25 '18 at 18:42
  • @pbarranis [private JavaScript fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_fields) are actually private and there is no way to access them from within the JavaScript runtime. – Yogu Nov 22 '19 at 12:38