Advertisement
Beginner TypeScript, Modules

How do you import just the types from a module without importing any runtime code, using 'import type'?

Short answer

'import type' explicitly imports only type information, which the compiler completely erases from the output JavaScript — useful for making it clear an import has zero runtime cost, and required in some strict module setups (isolatedModules) to disambiguate type-only imports.

Save TypeScript mock interview
Advertisement
AModel answer
'import type' explicitly imports only type information, which the compiler completely erases from the output JavaScript — useful for making it clear an import has zero runtime cost, and required in some strict module setups (isolatedModules) to disambiguate type-only imports.
import type { User } from './types';
import { fetchUser } from './api';

function display(user: User) { /* ... */ } // 'User' never exists in compiled JS output
Real-world example
Clearly signaling in a large codebase which imports are purely for type checking versus which pull in actual runtime dependencies.
Likely follow-ups
  • What happens if you accidentally use 'import type' for something you also need as a runtime value?
Related topics