Course contentsShow
← TypeScript
Lesson 1097 of 1,79626. Modules in TypeScriptFree lesson

Why type-only imports matter for bundlers

Understand how type-only imports help bundlers tree-shake and avoid circular dependencies.

Why Type-Only Imports Matter for Bundlers

What you'll learn: How type-only imports help modern bundlers produce smaller, faster bundles by safely removing type-related code.

The Bundle Size Problem

When you import types the regular way, bundlers face a dilemma: "Is this import actually used at runtime, or is it just for TypeScript's type checking?"

import { User } from './types';

function greetUser(user: User) {
  console.log(`Hello, ${user.name}!`);
}

The bundler sees the import but doesn't know if User is a type or a runtime value. To be safe, it might include the entire ./types module in your final JavaScript bundle—even though User was only used for compile-time type checking and disappears after TypeScript compiles.

Enter Type-Only Imports

With import type, you explicitly tell bundlers: "This import is only for types. Remove it completely from the output."

import type { User } from './types';

function greetUser(user: User) {
  console.log(`Hello, ${user.name}!`);
}

Now the bundler knows with certainty that ./types can be excluded from the bundle. No guessing. No bloat.

Tree-Shaking Benefits

Modern bundlers like Webpack, Rollup, and Vite use tree-shaking to eliminate unused code. Type-only imports make tree-shaking more aggressive and reliable because there's no ambiguity about what's type-level versus runtime.

Avoiding Circular Dependencies

Type-only imports also help break circular dependency chains. If Module A and Module B import each other, but some of those imports are only for types, using import type prevents the bundler from creating problematic runtime cycles.

Think of it like this: regular imports are "hard" dependencies (runtime needs), while type-only imports are "soft" dependencies (compile-time hints). Soft dependencies don't create circular loops in your runtime module graph.

Key Takeaway: Type-only imports tell bundlers exactly what can be safely removed, resulting in smaller bundles, better tree-shaking, and fewer circular dependency issues.