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
[consistent-type-imports] add option to autofix to a single type import #2769
Comments
Happy to accept a PR |
Please please please do this! |
This ads a new mode called "" for consistent-type-imports rule which will only trigger, if all imports of an import declaration are type only imports. References typescript-eslint#2769
This ads a new mode called "type-imports-mixed" for consistent-type-imports rule which will only trigger, if all imports of an import declaration are type only imports. References typescript-eslint#2769
Ok I think I got it working - this should prevent the rule from triggering when there are mixed imports |
This ads a new mode called "type-imports-mixed" for consistent-type-imports rule which will only trigger, if all imports of an import declaration are type only imports. References typescript-eslint#2769
This ads a new mode called "type-imports-mixed" for consistent-type-imports rule which will only trigger, if all imports of an import declaration are type only imports. References typescript-eslint#2769
This comment has been minimized.
This comment has been minimized.
With any issue in opened in this project - it either has a visible progress in the form of an attached PR, or it has no progress. We are a community run project. The volunteer maintainers spend most of their time triaging issues and reviewing PRs. This means that most issues will not progress unless a member of the community steps up and champions it. If this issue is important to you - consider being that champion. If not - please just subscribe to the issue and wait patiently. |
With the release of TS 4.5, it seems like updating the fixer to use inline type annotations (skipped in #4237 due to complexity) addresses the underlying issue here as you'd get: import { type ILogger, Logger } from './Logger';
import { type SomeInterface } from './SomeInterface'; |
yup - with 4.5 we can now autofix to use inline types to combine the imports. It's worth noting that with support for 4.5's inline type qualifier in #4237 - we don't explicitly need fixer support in the rule. Instead a rule like Eg the fix would be // original
import { ILogger, Logger } from './Logger';
// @typescript-eslint/consistent-type-imports
import type { ILogger } from './Logger';
import { Logger } from './Logger';
// import/no-duplicates
import { type ILogger, Logger] from './Logger'; OFC it'd be great if we had support in our rule to cut-out the middleman. |
Should there be a new issue to reflect that work and replace this one? I'm looking at the fixer logic now to see just how hard the augmentation would be. |
no we can keep this one - i've update this issue to match. |
Should it be
like the description or
The second version is more accommodating to additional imports from the same source with fewer changes necessary? |
IMO the former as it allows tools to ahead-of-time optimise the import away instead of having to interrogate all the imports to determine if it includes a type-aware import. eg compilers can do - import type { SomeInterface } from './SomeInterface'; instead of // (1)
import {
- type SomeInterface
} from './SomeInterface';
// (2)
- import {} from './SomeInterface'; additionally tools that analyse imports (lint rules etc) can understand the file's dependencies easier for the same reason. |
IMO, it depends on the imports themselves. Currently I have a rule enforcing no duplicate imports, which means I can't use the import type { Foo } from "src/foo";
import { Bar } from "src/foo"; which duplicates imports. What I'd like to be able to do - independent of supporting inline import { Foo, Bar } from "src/foo"; because it recognizes that at least one import is a value so it cannot be an But to get back on-topic, the rule should, IMO prefer import type { Foo } from "src/foo"; over import { type Foo } from "src/foo"; because all imports are types, but should not raise an issue (optionally?) if one has import { Bar, type Foo } from "src/foo"; because at least one import is not a type. Also, this probably isn't the appropriate place to mention this, but Angular uses type annotations for dependency injection, so even if |
Why not? Why can't your duplicate import rule handle this and fix it appropriately? If you're using ESLint's core
This is supported - if you use type-aware linting. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as spam.
This comment was marked as spam.
With any issue in opened in this project - it either has a visible progress in the form of an attached PR, or it has no progress. We are a community run project. The volunteer maintainers spend most of their time triaging issues and reviewing PRs. This means that most issues will not progress unless a member of the community steps up and champions it. If this issue is important to you - consider being that champion. If not - please just subscribe to the issue and wait patiently. |
The This won't deduplicate your import declarations, however. If you want then, then you can then use a rule like You can also use |
I'd like to propose a third option for the consistent-type-imports rule's
prefer
property. It currently has two options forprefer
:type-imports
andno-type-imports
. However, it would be very useful to have a third option:import type
except when there's already a concrete import from that same file, in which case prefer to combine the imports into a single statement.Here's an example:
Logger.ts
main.ts
prefer: 'type-imports'
prefer: 'no-type-imports'
New proposal:
prefer: 'type-imports-combine'
The text was updated successfully, but these errors were encountered: