typescript-eslint: Configs: Make consistent-type-imports 'error' for the recommended config
Before You File a Proposal Please Confirm You Have Done The Following…
- I have searched for related issues and found none that match my proposal.
- I have read the FAQ and my problem is not listed.
Description
It seems to me that using the @typescript-eslint/consistent-type-imports rule to enforce the use of import type where possible:
- Is beneficial (this blog post by maintainer @JoshuaKGoldberg explains it well)
- Is easy (
eslint --fixjust does the thing you want automatically) - And has no meaningful drawbacks.
Given all of that, is there any reason not to add it to the recommended configs?
Impacted Configurations
recommended-type-checked recommended-type-checked-only strict-type-checked strict-type-checked-only
Additional Info
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 25 (24 by maintainers)
STRONGLY disagree with that sentiment. We use TS as our build tool yet we use type-only imports.
It’s a very useful syntax as it allows you to explicitly annotate things which makes code clearer. Especially for reviewers within github that don’t see the entire file and/or don’t have type information /intellisense.
Its the explicitness of declaring “this import will be elided” that adds clarity and predictability to the code.
I’m not sure I see that POV. The point of the stylistic ruleset is that we are giving users an opinionated set of rules that we recommend that may not fit everyone’s preferences.
The rule could be considered a style, sure, but it is a strict style that enforces some invariants, unlocks better tooling and makes things easier. TBH I personally would say that the rule itself is a correctness rule and the only stylistic thing is the fixer style.
Ultimately this is in fact a stylistic concern that does not change a thing in many codebases:
Add to that the fact that you really need a fixer for this rule - and no matter what you choose that fixer is enforcing a stylistic choice. Which is why the fixer is configurable.
Add to that the fact that out-of-the-box any codebase using legacy decorators +
emitDecoratorMetadatawill get incorrect reports without additional config.I don’t think this is a good candidate for
recommended. So I’m a strong -1 for this being there.But definitely +1 it should be on in strict/stylistic.
@kirkwaiblinger I thought I was testing with the latest version, but seems that I was using 7.7.1. I’ll test with the latest. Thanks for pointing that issue!
I’m trying to run just this rule in my code base, usually linting takes 40 seconds. But trying to get errors for this one rule takes over 10 mins 🙈 Probably because there is so many errors.
ACK that in projects where build predictability and knowing what gets elided are important,
consistent-type-imports: "no-type-imports"is good and useful. My pushback is that I think there are many projects where those don’t matter. For many projects, most files don’t have side effects and they don’t have build predicatability or runtime performance issues. So the net benefit of a stylistic rule like this is … roughly zero. No real measurable impact on day-to-day editing, or debugging ease, or product quality.The drawbacks of enabling
consistent-type-importsto"no-type-imports"on those projects are:If we had a
stylisticsplit between the “starter” and “comprehensive” configs the wayrecommendedandstrictare split, I’d say this might make sense in the “comprehensive” version. But for a standard starter likestylistic, I think it doesn’t pass the bar of “would this go against the desired pattern for a nontrivial percentage of projects that are set up well”.If you don’t care about which imports will be elided, then that clarity and predictability generally don’t mean much. In an app where everything gets bundled into optimized chunks for production, what tangible benefits does this give?
I think I need to check this with better time, but at least quickly I was not able to get it working.
emitDecoratorMetadatais not set by default, so I think that rule fix does not work out of box for Angular app. You can see their recomended tsconfig here: https://angular.io/guide/typescript-configuration - I think in some older versions it was used, but they stopped using it some point.And if someone wants to test, I think this example project could be used from this page https://angular.io/tutorial/tour-of-heroes/toh-pt4
I tried to add emitDecorationMetadata to
.eslintrc.jsonbut it may be that I’m doing this wrong and it does not work like this with non-flat config? Also, using ts version 4.9.5. That said, I think this sounds like separate issue and I don’t want to spam this discussion with side note investigation 😃