-
Notifications
You must be signed in to change notification settings - Fork 887
tslint:recommended interface-name uses always-prefix #1520
Comments
@spiffytech Although there are style-guides that don't recommend using the I prefix, from my experience, it's not necessarily true that "the broader community's practice" is to follow these recommendations. When the interface is used purely for typings of external modules ( However, when writing your own code base, the interfaces often have a different semantic meaning in which they actually define an an API to be follow, a contract without any implementation. I think that when writing a library using TypeScript from scratch, this is the more common use case. |
It also can be helpful to have interfaces visually distinguished when looking at import statements: imports that only import interfaces are elided by the TS compiler. This can affect your code if the module import would've had some other side-effect. I'm somewhat ambivalent overall though |
I find the vast majority of my uses of interfaces are to describe (possibly extended) objects: eg options, react props, JSON config files, REST responses, etc. Using a prefix in this case is quite confusing. Should I instead be using I get the feeling that if you have a lot of If I had to pick "always" or "never" I'd lean towards "never", but at the moment I'd say it depends on the code base too much. Really, I would say the recommended rule should be something like "only-with-matching-class" - eg exported It would be interesting to see an analysis of open typescript code to see if there is a strong existing preference though. Something to note, the typescript compiler itself seems to generally uses public interface |
FYI, TypeScript contributors are also asked to not prefix interface names: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines |
Instead of picking "always" or "never" as default, can you just remove it from recommended? As far as I know the |
I'm patching winston v2.x type definition file to make the type checker feels happy with the actual winston v3.x in node_modules. I like the IMO, we should remove this rule from tslint:recommended. It's holding me back while developing, then I reached this issue :( |
The Java JDK libraries do not use Hungarian notation, see lang packages etc... Some good reasoning against here: Not a fan of it, and having it required as a default encourages developers to stay away from interfaces all together so I would argue there's a strong case for |
FWIW, reasoning in favor of interface INameValidator {
validate(name: string): boolean;
}
class NameValidator implements INameValidator {
public validate(name: string): boolean { /* ... */ }
} |
I would call my interface ‘Validator’ as my implementation is usually specific and my interfaces are more abstract. But these interfaces are typically for actual use, not testing. With testing you don’t have to formally declare an interface to test it. As I said, I would rather ‘recommended’ not take a stance on it either way - neither always nor never. Just remove it. This is a personal / organization preference, not a best practice. |
Agreed, let's get this out of recommended. 😊 |
Related superset issue: #4305 |
Removing the |
tslint:recommended
defaults toalways-prefix
. However, the authoritative sources I can find all discourage this behavior.I cannot find documented rationale for tslint's choice to recommend
always-prefix
despite being at odds with the broader community's practice.Please document the rationale for this, and/or consider changing the default to
never-prefix
.The text was updated successfully, but these errors were encountered: