Skip to content

"Typescript: Remove unused imports" does not take into consideration if an import is valid or not #252653

Open
@kneczaj

Description

@kneczaj

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.101.2
  • OS Version: MacOS 15.5

Steps to Reproduce:

  1. Get project with Typescript
  2. Import an object - const or function from a file which does not exist, so the import is invalid
  3. In next line import the same object but now from a file where it exists, so the import is valid
  4. Run ">Typescript: Remove unused imports" command.

Example code ("./mocks.ts" does not exist):

src/modules/A/mocks.ts:

export const A = 'A';

src/test.ts:

import { A } from './mocks';
import { A } from 'src/modules/A/mocks';

console.log(A)

Current effect:
The command will leave the first invalid import, and remove the second valid import.

Expected:
The valid import (second) should stay, and the invalid (first) should be removed.

Comments:
It looks like the command does not take into consideration if the import is valid or not.

Use cases:

  1. Resolving merge conflicts. After both merge changes are accepted the result mixes valid and invalid imports. "Typescript: Remove unused imports" should then remove import duplicates, but leave the valid ones and remove invalid ones, not the other way.
  2. Fixing imports by hand. After adding a new valid import by hand at the end of imports "Typescript: Remove unused imports" will remove it and leave the invalid one

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions