The future of the "private" keyword
See original GitHub issueThis is a somewhat “catch-all” issue to serve as a place of discussion for a question that is sure to come up given the advancement of Private-Named Instance Fields:
What is the future of the “private” keyword in TypeScript?
That’s the general question - following are some short-and-sweet Q&As that I’ve pulled up for visibility.
Is TypeScript going to depreciate it, and aim towards phasing it out in favor of the private field operator?
Our current plan is to leave the current private behavior as-is.
Is it going to be supported in the form of a transformer , turning private bar
into #bar
?
definitely “no” because that would require type-directed emit.
A few more questions:
Would #-fields allow modifiers like readonly?
Will public #x, private #x and protected #x be an error?
Will it be possible to assign #-fields from the constructor parameters, the same way as the current parameter-properties work (i.e., constructor (private x) and constructor (#x))?
What visibility rules will apply between private entities and #-entities? What if I use a private-entity from a #-entity, or vice versa? Seems that #-entites are ‘more’ private than private-entities, in some sense. 😃
The visibility of a method doesn’t change which properties it’s allowed to access
Would it be possible to do the following:
class Foo {
bar: string;
#baz: number;
}
const foo: Foo = { bar: 'bar' };
Issue Analytics
- State:
- Created 4 years ago
- Reactions:28
- Comments:67 (22 by maintainers)
Top Results From Across the Web
class - What are the differences between the private keyword ...
The private keyword in TypeScript is a compile time annotation. It tells the compiler that a property should only be accessible inside that ......
Read more >Why do we need private variables?
Private variables ensure that later references outside the scope of an object or function won't inadvertently affect other variables.
Read more >EcmaScript vs TypeScript — Private Fields - Level Up Coding
In TypeScript, we already have the public and private keywords. But now in the recent 3.8 version, TypeScript introduced the EcmaScript private ...
Read more >Public, Protected and Private Keywords - InformIT
The private keyword denotes the least stable kind of method and provides the most restricted visibility. Private methods must be called with ...
Read more >What is private used for? - The Healthy Journal
The private keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.
Read more >
Top Related Medium Post
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I would personally prefer to keep the
private
keyword. To me, it looks waywayway better than#
. And is more clear in its intent, to me. And I wouldn’t want to have to go back and do a regex replace forprivate
to#
.When I found out
#
was being proposed instead of justprivate
, I thought I was experiencing the Mandela effect.Our current plan is to leave the current
private
behavior as-is.Reasons for this:
private
private
fields can be read from unit tests (I realize some people find this personally distasteful, but this is not universal)private
fields have much better runtime perf in downlevel scenarios#
syntax so why force it on themWeakMap
(which not all runtimes have) there’s no good equivalent downleveling for hard runtime privacyAs for a transformer, probably not? It’s simple to replace these with a regex if you’re motivated.