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
Related issue: vitejs/vite#4581
Repro: esbuild-type-import-repro
In TypeScript modules type imports are not removed, unless
importsNotUsedAsValuesis"remove".Given
tsconfg.json
{ "compilerOptions": { "noEmit": true, "isolatedModules": true, "importsNotUsedAsValues": "error" } }module.ts
main.ts
ESBuild emits
Which is not correct, because valid TypeScript is transformed into invalid JavaScript.
Note that
tscdoes 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
woud be transformed into
Related issue: vitejs/vite#4581
Repro: esbuild-type-import-repro