-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal for new rule - consistent style for type-only imports #2200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is technically possible to build right now, but it will be much easier once we release the scope analyser: #1939 |
I would love to have such a rule, with per-package configuration. For some packages I only want to allow type-only imports to ensure the package never ends up in the runtime bundle. |
This is a duplicate though of #1985, right? |
they're not quite the same. #1985 is about building a rule which requires that all types are imported via They could probably be merged together, as technically this request will cover #1985 (but #1985 won't cover this request). For example, this case would error in this propsed rule, but not in #1985, because import { Component } from 'react';
// error: Component is only used as a type, so it should be imported using a type only import
type Test = Component; |
I'll merge the two into this one. requirements for rule:
|
That wasn't my exact intention for proposing this rule. The intention was to enforce one style of type imports. This enforced style could be a type import, but it could also be an import in a type annotation or just a regular import.
Would these rules also allow to prefer imports in type annotations instead of using regular imports or type imports? |
It could. Within TS code that style is ridiculously verbose and the repetition required very much goes against every programming principle I've ever learnt. |
This rule would be useful to me, particularly in conjunction with eslint-plugin-import's no-cycle rule. no-cycle works well, but is not smart enough to inspect imported symbols to distinguish between types and values. It flags type-only imports as cyclic dependencies, but I don't really care about those, since they have no runtime impact, while other cyclic dependencies can cause bugs. If we had a rule to enforce the |
I actually think we could accomplish this right now, but I know our current scope analyser is wildly inconsistent and inaccurate. Once #2039 is merged, that will no longer be the case, but That really needs to wait for the 4.0.0 release as it's a pretty big change and is kinda breaking as it will report a lot more errors for users, and likely will require some reconfiguring for them.. Note that due to the direction of the ecosystem, we're going to have to make a few breaking changes to our AST very soon (read: likely within a month - exact timing depends on eslint/eslint#13416). If someone wants to start building this on top of #2039, then we would be in a good state to ensure it is released as soon as 4.0.0 is ready. |
This has been released to NPM as part of the prerelease version for v4. Try out the Please raise any bugs you find as a new issue so we can track and fix them before the full release! https://github.com/typescript-eslint/typescript-eslint/milestone/5 |
Hi @bradzacher, perhaps I'm missing something but what is the difference between this proposal and #1786 which was closed for duplicating an existing TS error? The reason I ask is I'm considering proposing another TS 3.8 type syntax rule that also duplicates an existing TS error ( |
A few reasons I can see:
In hindsight - for this case I don't mind having this rule. For your request, I might lean toward this being implemented in the TS language server for a few reasons:
|
Fair call! I agree that it would make sense for typescript to handle this since it's an actual typescript error. The downside is that, as far as I'm aware, tsc/ts language server doesn't have the nice auto fixing functionality that eslint has where you can run it in bulk across a whole project. We're currently switching the isolatedModules flag on and have a ton of violations that I was hoping to fix somehow, perhaps I can write a short script that calls the TS language server directly to replicate the bulk fixing functionality of eslint. |
For one-off migrations I would recommend a proper codemod tool like jscodeshift. |
Proposal
Expected Result
FooBar is only being used as a type, import as 'import type {FooBar}'
Actual Result
Additional Info
Since typescript now supports
import type {X}
for type only imports that don't get emitted in js, nor do get counted as cyclic imports, it would be nice to have typescript-eslint automatically flag and fiximport {T}
toimport type {T}
If you're willing to accept the suggestion, I'm happy to create a PR and land this
The text was updated successfully, but these errors were encountered: