Skip to content

Enhancement: consistent-type-imports Fix type imports used as values #11527

@eliasm307

Description

@eliasm307

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existsenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions