Namespace Imports and Types
You'll learn: How to import an entire module as a single object and access both its types and values through a namespace-like structure.
What Are Namespace Imports?
When a module exports many things—functions, classes, constants, and types—you can import them all at once under a single name using namespace import syntax. Instead of listing each export individually, you create a container that holds everything the module exports.
import * as Utils from './utils';
This Utils object now acts like a namespace: you access everything through it with dot notation: Utils.formatDate(), Utils.User, etc.
Why This Matters for Types
The powerful part: namespace imports work identically for both values and types. If a module exports a type, interface, or type alias, you access it through the same namespace object:
// utils.ts
export interface User {
id: number;
name: string;
}
export function formatUser(user: User): string {
return `${user.name} (#${user.id})`;
}
// main.ts
import * as Utils from './utils';
const user: Utils.User = { id: 1, name: 'Alice' };
const result = Utils.formatUser(user);
Notice Utils.User is a type, while Utils.formatUser is a value—both accessed the same way.
When to Use Namespace Imports
This pattern shines when:
- A module has many related exports and you want logical grouping
- You want to avoid naming conflicts without renaming individual imports
- The module name itself provides useful context (like
Math.floor(),JSON.parse())
Key Takeaway: Namespace imports (import * as Name) let you bundle all of a module's exports—both types and runtime values—under one name, accessed with dot notation, providing organization and preventing name collisions.