-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
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 searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Link to the rule's documentation
https://typescript-eslint.io/rules/consistent-type-imports/
Description
The consistent-type-imports
includes a fixStyle
option which is for converting a value import into a type import "when an import is detected as used only in the type position", however, the rule does not check/fix the opposite way ie when a type import is detected as used NOT only in the type position.
This is not ideal because the rule can create some awkward DX for things that can be used as both types and values depending on how the code changes over time e.g. imagine someone is writing code and they start with code like
import {SomeEnum, SomeClass} from 'foo'
let enum1: SomeEnum | undefined
let instance1: SomeClass | undefined
at this point if the rule runs it will auto fix the imports to be type imports (e.g. import {type SomeEnum, type SomeClass} from 'foo'
), however if more code is written and it becomes:
import {type SomeEnum, type SomeClass} from 'foo'
let enum1: SomeEnum | undefined
let instance1: SomeClass | undefined
if(someCondition) {
enum1 = SomeEnum.Value
instance1 = new SomeClass()
}
if the rule runs again now it will do nothing, however there would be a ts issue as type only inputs are used as values, however the dev did not add those type imports, the lint rule did, but its up to the dev to manually fix this case which is inconvenient.
Ideally, the rule should also fix the code to remove the type imports where necessary and handle cases where only some named imports need to be converted e.g.
import type {UsedAsType, UsedAsValue} from 'foo'
const v: UsedAsType = UsedAsValue;
Fail
import {type UsedAsType, type UsedAsValue} from 'foo'
const v: UsedAsType = UsedAsValue;
Pass
import {type UsedAsType, UsedAsValue} from 'foo'
const v: UsedAsType = UsedAsValue;
Additional Info
No response