Importing Types Alongside Values
What you'll learn: How to import both runtime values and types from the same module in a single statement.
The Core Idea
When working with modules in TypeScript, you often need both runtime code (functions, classes, objects) and type definitions (interfaces, types, type aliases) from the same file. Instead of using separate import statements, TypeScript lets you import them together in one clean line.
Think of it like ordering from a restaurant menu: you can order both food (runtime values) and the nutritional information sheet (types) in a single order, rather than making two separate requests.
How It Works
The syntax is straightforward—you simply list types and values together in the same import statement:
// user.ts
export interface User {
id: number;
name: string;
}
export function createUser(name: string): User {
return { id: Date.now(), name };
}
// app.ts
import { User, createUser } from './user';
const newUser: User = createUser('Alice');
Here, User is a type-only import (exists only at compile-time), while createUser is a value (exists at runtime). Both come from the same module, imported in one statement.
Why This Matters
You could theoretically separate them, but combining type and value imports keeps your code cleaner and your import section more concise. It reflects how you naturally think about modules: as cohesive units that provide both behavior and type information.
Key Takeaway: TypeScript allows you to import types and runtime values from the same module in a single import statement, keeping your imports organized and your code readable.