Namespace Imports and Types
You'll learn: How to import all types from a module using namespace syntax without creating runtime overhead.
What Are Namespace Imports for Types?
When working with modules that export many types, you might want to group them all under a single namespace identifier rather than importing each one individually. TypeScript offers import type * as Name syntax specifically for this purpose — it's like creating a "type namespace" that only exists at compile time.
Think of it like organizing books in your library. Instead of carrying individual books from the library to your desk (import { Book1, Book2, Book3 }), you might ask for "everything from the TypeScript shelf" bundled together (import * as TypeScriptShelf). When you only need the table of contents (types) and not the physical books (runtime values), you use import type * as to signal this to the compiler.
The Syntax
import type * as Types from './module';
This brings in all exported types from the module, grouped under the Types namespace, with zero runtime code generated.
Why This Matters
Traditional namespace imports (import * as Name) create a runtime object that holds all the module's exports. But if you're only using types from a module, this creates unnecessary bundle bloat. Using import type * as tells TypeScript and your bundler: "I only need these for type-checking — eliminate this import entirely at runtime."
// Runtime import - creates a module object
import * as Utils from './utils';
// Type-only namespace import - zero runtime code
import type * as UtilTypes from './utils';
Real-World Usage
Imagine a types-only module:
// types.ts
export type User = { id: number; name: string };
export type Product = { id: number; price: number };
export type Order = { userId: number; productId: number };
You can import all these types under one namespace:
// app.ts
import type * as Models from './types';
function processOrder(order: Models.Order): void {
// Use Models.User, Models.Product, Models.Order
}
Key Takeaway: Use import type * as Name when you need multiple types from a module grouped under a namespace, ensuring the import disappears completely at runtime for optimal bundle size.