The import type Syntax
What you'll learn: How to explicitly import TypeScript types without including them in your compiled JavaScript output.
Why It Exists
When you import something in TypeScript, you might be importing an actual value (like a function or class) or just a type (like an interface). Regular imports bring both into scope, but this creates a problem: bundlers and the TypeScript compiler might accidentally include type-only dependencies in your final JavaScript bundle, even though types don't exist at runtime.
The import type syntax solves this by explicitly marking imports as type-only, guaranteeing they'll be completely erased during compilation.
The Basic Syntax
Instead of this regular import:
import { User } from './types';
You write:
import type { User } from './types';
This tells TypeScript: "I'm only using User as a type annotation. Remove this import entirely from the compiled output."
When to Use It
Use import type when you're importing:
- Interfaces
- Type aliases
- Types extracted from other modules
- Anything that only exists at compile-time
This is especially important when working with bundlers (like webpack or Rollup) that perform tree-shaking and dead-code elimination. A regular import might create a false dependency that prevents proper optimization.
The Safety Guarantee
With import type, TypeScript enforces that you can't accidentally use the imported name as a value. This prevents runtime errors and keeps your intentions clear to both the compiler and other developers reading your code.
Key Takeaway: Use import type { ... } to explicitly import types without affecting your runtime bundle, ensuring clean separation between compile-time types and runtime code.