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

Why Type-Only Imports Exist

Understanding how TypeScript separates types from runtime code and why this matters for imports.

Why Type-Only Imports Exist

You'll learn: How TypeScript separates types from runtime code and why this matters for imports.

The Fundamental Split

TypeScript has a critical distinction: types exist only at compile-time, while values exist at runtime. When you import something from a module, it could be a type (interface, type alias), a value (function, class, constant), or both.

The problem? Your bundler and JavaScript runtime don't know the difference. They treat all imports as if they create runtime dependencies, even when you're only importing something that will be erased during compilation.

The Real-World Impact

Imagine you import a large type definition from a module just to annotate a variable. Without type-only imports, bundlers like webpack or esbuild see that import statement and include the entire module in your bundle—even though that type disappears after compilation.

This creates three problems:

  1. Larger bundles: Dead code gets included
  2. Circular dependency risks: Type-level imports might create cycles that break module loading
  3. Ambiguity for tools: Build tools can't reliably determine what's safe to remove

Think of It Like This

Consider a restaurant menu (types) versus the actual ingredients (runtime values). You reference the menu to decide what to order, but the menu itself doesn't end up on your plate. Type-only imports are like telling your bundler: "I'm just reading the menu, don't ship it with my meal."

What TypeScript Does

By using import type, you explicitly tell TypeScript and your build tools: "This import is only for type-checking. Erase it completely from the JavaScript output."

This enables aggressive tree-shaking, prevents false circular dependencies, and makes your build process more efficient.

Key Takeaway: Type-only imports exist because TypeScript needs a way to tell bundlers and build tools which imports are purely for type-checking and can be safely removed from the final JavaScript bundle.