Skip to content

Modules in TypeScript

Organize code with ES modules: import, export, default exports, and re-exports.

What you'll learn

  • How export and import work in TypeScript modules
  • Default exports vs named exports
  • Re-exporting and barrel files (index.ts)
  • Type-only imports and exports with the type keyword

Concept

ES Module Syntax

TypeScript uses the standard ES module system. You export with export and import with import. Named exports allow multiple exports per file: export function add() and export const PI. You import them by name: import { add, PI } from "./math". Every .ts file is a module — its top-level declarations are local unless exported. This encapsulation prevents global namespace pollution.

Default Exports

A default export is the single "main" export of a module: export default class User {}. You import it without braces and can rename it freely: import MyUser from "./user". Default exports are convenient but can make refactoring harder because the import name is arbitrary. Many style guides recommend preferring named exports for consistency and better IDE support (auto-import, find references).

Type-Only Imports

When you import only types (which are erased at compile time), use import type: import type { User } from "./types". This tells the compiler the import is purely a type and should not appear in the compiled JavaScript. This avoids importing runtime code that is only needed for types, which can cause circular dependency issues or unnecessary bundle size. You can also inline it: import { type User, addUser } from "./user".

Example

              // --- math.ts ---
export const PI = 3.14159;
export function add(a: number, b: number): number {
  return a + b;
}
export default function multiply(a: number, b: number): number {
  return a * b;
}

// --- types.ts ---
export interface User {
  id: number;
  name: string;
}

// --- main.ts ---
import multiply, { add, PI } from "./math";
import type { User } from "./types";

// Re-export (barrel file pattern).
// export * from "./math";
// export * from "./types";

const user: User = { id: 1, name: "Alice" };
console.log(add(2, 3), multiply(4, 5), PI, user.name);
            

Named exports allow multiple per file; default export is the single main export. Use import type for type-only imports to keep them out of the compiled output. Barrel files (index.ts) re-export from multiple modules.

Try it

  • Code Minifier

    Minification removes unused exports — see how module tree-shaking works.

  • Code Diff

    Compare module-based code with its compiled JavaScript output.

Common mistakes

The mistake

Mixing default and named exports from the same module inconsistently

The fix

Choose one style per module and stick with it. Named exports are generally preferred for better refactoring support and explicit import names.

The mistake

Importing types without 'import type' causing circular dependency errors

The fix

Use 'import type { X }' for type-only imports. This ensures the import is erased at compile time, breaking circular dependencies that exist only at the type level.

Practice Modules in TypeScript

2 exercises

Practice Modules in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors