typescript-eslint: Configs: Make consistent-type-imports 'error' for the recommended config

Before You File a Proposal Please Confirm You Have Done The Following…

Description

It seems to me that using the @typescript-eslint/consistent-type-imports rule to enforce the use of import type where possible:

  1. Is beneficial (this blog post by maintainer @JoshuaKGoldberg explains it well)
  2. Is easy (eslint --fix just does the thing you want automatically)
  3. 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)

Most upvoted comments

So asking for a slightly more verbose style that includes type really just makes the code less readable.

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.

There’s no one single style that fits everyone.

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:

  • if you transpile with TS then using type-qualifiers doesn’t change anything.
  • if you transpile with non-TS tools then this may or may not change anything (tools like SWC will do a good job of guessing and eliding imports only used in type locations).

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 + emitDecoratorMetadata will 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-imports to "no-type-imports" on those projects are:

  • It results in overly verbose imports, taking up code space
  • It adds conceptual overhead: developers need to do slightly more work to read the imports

If we had a stylistic split between the “starter” and “comprehensive” configs the way recommended and strict are split, I’d say this might make sense in the “comprehensive” version. But for a standard starter like stylistic, 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”.

Its the explicitness of declaring “this import will be elided” that adds clarity and predictability to the code.

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. emitDecoratorMetadata is 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

For the sample application that this page describes, see the live example / download example.

I tried to add emitDecorationMetadata to .eslintrc.json but 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 😃

  "parserOptions": {
    "project": [
      "tsconfig.app.json"
    ],
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },