If your bundler requires it, then you should use the import type
syntax. According to information from the official documentation:
...this allows non-TypeScript transpilers like Babel, swc or esbuild to determine which imports can be safely removed.
In cases where this is not necessary, simply using import
will suffice:
import { Foo, BarValue } from "./types";
However, if you find that you do require it, starting from version 4.5, you have the option to use the type
prefix on specific parts of the import
:
import { type Foo, BarValue } from "./types";
For what it's worth, I frequently use regular import
statements that bring in both types and values without any issues, even in projects with isolatedModules: true
set in the tsconfig.json
. The bundlers I work with (mostly Vite, but also Webpack) do not necessitate the use of import type
.
To confirm, I conducted a test in a brand-new project:
tsconfig.json
{
"compilerOptions": {
"isolatedModules": true
}
}
types.ts
export type Foo = {
a: number;
};
export const BarValue = 42;
main.ts
:
import { Foo, BarValue } from "./types";
const a: Foo = {
a: 42,
};
console.log(BarValue);
console.log(a);
The tsc
command executed successfully and generated the expected types.js
and main.js
files. I verified that it was able to output either ESM or CommonJS with the standard import
statement (without including the type portion).