typescript-eslint: [explicit-member-accessibility] should not be part of recommended
I have just noticed that currently we include
"@typescript-eslint/explicit-member-accessibility": "error",
in recommended.json from eslint-plugin.
I propose that we remove it.
Rationale
TypeScript has always positioned itself as an extension of JavaScript. It is no secret that I think it does a bloody good job at it đ
With it being an extension of JavaScript, there is an expectation that the majority of what you write is JavaScript code, and any syntactic additions provided by TypeScript are there to give you functionality you otherwise do not have.
I strongly believe this does not apply to the public access modifier.
Classes have long been supported in JavaScript, and developers should understand how they work at runtime, regardless of if they use TypeScript or not. As we know, class properties are public in JavaScript.
The private and protected access modifiers do give you functionality you (currently**) otherwise do not have, so adding them to your otherwise valid JavaScript classes is a great capability.
The public access modifier is not only unarguably functionally redundant based on my previous assertions about developers needing to understand JavaScript classes, it is arguably visual clutter when reading the code, and makes it harder to read.
Readability is always subjective, I admit that, but I posit it is harder because having two words (public and private) both beginning with p, both of roughly equal length in front of every single class property makes them less likely to be explicitly distinguishable from one another when scanning code.
An interesting piece of context to this - I often see one of the main reasons people use to justify not using TypeScript is that it is âso different to JavaScriptâ and âlooks like Javaâ. This is absolutely not the case when you approach it in the way I suggest above, and leverage it as an extension to JavaScript. Having a high-profile project like this one recommend a rule like this to developers does not help with that image IMO.
Finally, the rationale for the rule given in the description in the README of the rule is very unclear.
It is conflating the decision of whether or not things should be private to a class, with whether or not public things should use a public access modifier.
Leaving off accessibility modifier and making everything public can make
your interface hard to use by others.
If you make all internal pieces private or protected, your interface will
be easier to use.
These are two completely different things:
Making everything public != Not using the public keyword
I do understand why some people might want to use this rule, and I am not advocating that we should not support it.
I am however, strongly suggesting that it is not appropriate to include it in recommended.
If people wish to provide counter-arguments to what I have said above please do, but please remember keep the focus on the justification as to why it should be in recommended.
**I say currently because of course private properties are coming to JavaScript very soon, and they have completely different syntax to what we are used to with TypeScript today. TypeScript will adapt to this in one way or another, but by leveraging public it means you are doubling down on custom TypeScript syntax in the interim, and so that is also a factor IMO
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 27
- Comments: 19 (18 by maintainers)
I personally like them, because Iâm a sucker for being explicit.
Also coming from someone who has used C#/Java, I like being explicit because without it your code falls into the âdefault member accessibilityâ world which has a set of rules depending on where the caller is in relation to the callee.
I hate the proposed JS-standard private syntax. Iâve read and understand the reasoning behind it, but I still hate it. I would hazard a guess that typescript will look to transpiling
privateinto that syntax?The reason it was added originally was because the majority (if not all) of the community rulesets we looked at had it turned on. None of the ~5-10 community members that looked at the lists in the post objected to that rule, so it stuck.
what do you think about making 3 configs:
with that we are giving users a little more granularity and easier way to set up environment
Yes, this is how Java/C# work - there are tables you can look up to figure out what âdefaultâ actually means based on call context. It doesnât apply to the JS/TS world, sure, but itâs the reason I learned to be explicit about my accessibility modifiers, and since then I have maintained that practice because it means your code is clear and easy to understand.
Following from that, I like this rule because it forces people to be consistent in a codebase. Without the rule, you have a mish-mash of people maybe using private/protected, but on the whole theyâre going to always use nothing, defaulting to public, or esp if pure JS devs hop into the codebase, youâre likely to get public methods with
_underscorePrefixednames.Whilst
publicprovides no additional functionality over nothing, the rule as a whole does because it forces you to take a second to think about the code youâre writing. It forces developers of the codebase to think about how they are setting up the contracts in their code, rather than just defaulting everything to public.This is especially good when developing code for others to consume, as defaulting everything to public means that itâs a breaking change to bring it private.
IMO enforcing a consistent accessibility modifier is a best practice, and should be recommended.
Thatâs an interesting read, thank you. Iâd have to read more to understand the differences between the two, but it seems like the typescript team are saying that thereâs difficulties in that transpilation.
Which makes me think that there is definite use cases for continuing to use
privateinstead of#? Need to read more!Are you describing behaviour in those other languages here?
If so, I donât think it should be a factor in this. Recommended shouldnât be about making developers from other languages feel at home IMO