Skip to content

type imports are not removed #1525

@swandir

Description

@swandir

In TypeScript modules type imports are not removed, unless importsNotUsedAsValues is "remove".

Given

tsconfg.json

{
  "compilerOptions": {
    "noEmit": true,
    "isolatedModules": true,
    "importsNotUsedAsValues": "error"
  }
}

module.ts

export type Type = unknown;
export const runtimeValue = undefined;

main.ts

import { Type, runtimeValue } from "./module";

export const value = runtimeValue as Type;

ESBuild emits

import { Type, runtimeValue } from "./module";
export const value = runtimeValue;

Which is not correct, because valid TypeScript is transformed into invalid JavaScript.

Note that tsc does not emit an error in that case despite what one might think "importsNotUsedAsValues": "error" means.

Basically, type imports should be removed always.

Update:
To clarify: import statements should be preserved, but imported types should still be removed.
So this code

import { Type } from "./module";

woud be transformed into

import "./module";

Related issue: vitejs/vite#4581
Repro: esbuild-type-import-repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions