Type-Only Import Best Practices
You'll learn when and how to use type-only imports to write cleaner, more efficient TypeScript code.
Why Type-Only Imports Matter
When you import types in TypeScript, you're often bringing in information that only exists at compile-time and disappears entirely when your code runs. Type-only imports make this distinction explicit, helping both developers and build tools understand that certain imports are purely for type-checking.
Think of it like labeling boxes during a move: you mark "fragile" or "kitchen items" not because the label changes what's inside, but because it helps everyone handle the box correctly. Type-only imports label certain imports as "types only — discard after compilation."
When to Use Type-Only Imports
Use import type when:
- Importing interfaces or type aliases — These are always compile-time only:
import type { User, ApiResponse } from './types'
- Avoiding circular dependencies — Type-only imports don't create runtime module dependencies
- Making intentions clear — Signals to readers that this import won't affect runtime behavior
- Bundle size optimization — Helps bundlers tree-shake more effectively
Don't use import type when:
- Importing classes you'll instantiate — Classes exist at runtime
- Importing functions, constants, or enums — These are runtime values
- Importing something used in both type and value positions — Use mixed imports instead
Clarity and Code Review Benefits
Type-only imports make code reviews easier. When you see import type, you immediately know this module isn't contributing to runtime bundle size. It's documentation that the compiler can verify.
Key Takeaway: Use import type for interfaces and type aliases to improve code clarity and help bundlers eliminate dead code. Avoid it for values you'll use at runtime, and embrace it as self-documenting code that signals compile-time-only dependencies.