TypeScript Import and Export
Import and export statements help divide TypeScript applications into separate files. They allow developers to share classes, functions, interfaces, types, and other members between modules.
1. What Are Import and Export?
In a modern TypeScript application, each file can act as a module.
The export keyword makes selected members available to other
modules, while import allows a module to use exported members.
For example, in the CIIT Training Institute tutorial website, student-related functions could be placed in one file and reused by another file instead of writing the same logic repeatedly.
Module A
Defines and exports functions or types.
Import Statement
Connects exported members to another module.
Module B
Uses the imported functions or types.
2. Named Export
Named exports let a file expose multiple functions, variables, classes, interfaces, or types. The importing file uses the exported member names inside curly braces.
File 1: MathUtils.ts
export function add(
a: number,
b: number
): number {
return a + b;
}
export function subtract(
a: number,
b: number
): number {
return a - b;
}
export const appName: string = "CIIT TypeScript Tutorial";
File 2: App.ts
import { add, subtract, appName } from "./MathUtils";
console.log(appName);
console.log("Addition: " + add(20, 10));
console.log("Subtraction: " + subtract(20, 10));
Expected Output
CIIT TypeScript Tutorial
Addition: 30
Subtraction: 10
3. Default Export
A module can have one default export. The importing file can choose its own local name for that default export.
File 1: Student.ts
export default class Student {
constructor(
public name: string,
public course: string
) {}
display(): void {
console.log("Student Name: " + this.name);
console.log("Course: " + this.course);
}
}
File 2: App.ts
import Student from "./Student";
const student = new Student(
"Rahul",
"Full Stack Development"
);
student.display();
Expected Output
Student Name: Rahul
Course: Full Stack Development
4. Named Export vs Default Export
| Feature | Named Export | Default Export |
|---|---|---|
| Syntax | export function add() { } |
export default class Student { } |
| Import syntax | import { add } from "./MathUtils" |
import Student from "./Student" |
| Import naming | Must match the exported name unless aliased. | The imported local name can be chosen by the importer. |
| How many? | A module may have multiple named exports. | A module can have one default export. |
5. Import Aliases
An import alias lets you use a different local name for a named export.
Use the as keyword to create the alias.
File 1: StudentUtils.ts
export function display(): void {
console.log("Displaying student details");
}
File 2: App.ts
import { display as showStudent } from "./StudentUtils";
showStudent();
Expected Output
Displaying student details
The exported function is named display, but the importing file
accesses it using showStudent().
6. Import Multiple Members Together
Use import * as to access all named exports from a module
through a namespace-like object.
File 1: CourseUtils.ts
export const courseName = "TypeScript";
export function getDuration(): number {
return 3;
}
export function getInstitute(): string {
return "CIIT Training Institute";
}
File 2: App.ts
import * as CourseUtils from "./CourseUtils";
console.log(CourseUtils.courseName);
console.log(CourseUtils.getDuration() + " months");
console.log(CourseUtils.getInstitute());
Expected Output
TypeScript
3 months
CIIT Training Institute
7. Exporting Interfaces and Types
Interfaces and type aliases can be exported and reused by other modules. This helps keep data structures consistent across an application.
File 1: StudentTypes.ts
export interface Student {
id: number;
name: string;
course: string;
}
export type CourseStatus = "Active" | "Completed" | "Pending";
File 2: App.ts
import type { Student, CourseStatus } from "./StudentTypes";
const student: Student = {
id: 101,
name: "Priya",
course: "TypeScript"
};
const status: CourseStatus = "Active";
console.log(student.name);
console.log(student.course);
console.log("Status: " + status);
Expected Output
Priya
TypeScript
Status: Active
import type makes
it explicit that an import is only needed for types.
8. Re-Exporting Members
A module can re-export members from another module. This creates a central entry point for related functionality and makes imports easier to maintain.
File 1: Student.ts
export interface Student {
id: number;
name: string;
}
File 2: index.ts
export type { Student } from "./Student";
File 3: App.ts
import type { Student } from "./index";
const student: Student = {
id: 101,
name: "Amit"
};
console.log(student.name);
Expected Output
Amit
Here, index.ts re-exports the type from Student.ts,
and App.ts imports it through the central file.
9. Dynamic Import
Dynamic import() loads a module asynchronously and returns a Promise.
It is useful when a feature is only needed at a particular time.
File 1: Message.ts
export function showMessage(): void {
console.log("Welcome to CIIT Training Institute");
}
File 2: App.ts
async function loadMessage(): Promise<void> {
try {
const module = await import("./Message");
module.showMessage();
}
catch (error: unknown) {
if (error instanceof Error) {
console.log("Module loading failed: " + error.message);
}
}
}
loadMessage();
Expected Output
Welcome to CIIT Training Institute
10. Organizing Modules in a Project
A real application can organize code by responsibility. For example, a student management application might use a structure like this:
src/
│
├── students/
│ ├── Student.ts
│ ├── StudentService.ts
│ └── index.ts
│
├── courses/
│ ├── Course.ts
│ └── CourseService.ts
│
├── utilities/
│ └── MathUtils.ts
│
└── App.ts
students/
Contains student models and student-related services.
courses/
Contains course models and course-related logic.
utilities/
Contains reusable helper functions.
11. Common Import/Export Mistakes
Incorrect member name
A named import must match an exported name unless an alias is used.
Wrong file path
Verify relative import paths and filename spelling.
Confusing default and named exports
Use braces for ordinary named imports and no braces for a default import.
Importing type-only members as runtime values
Use import type when an import is only needed for type checking.
Missing export statement
A member must be exported before another module can import it.
Ignoring module configuration
Ensure the compiler and runtime support the module syntax being used.
Summary
exportmakes selected module members available to other files.- Named exports allow multiple members to be exported from a module.
- A module can have one default export.
- Use
asto create import aliases. import * asaccesses named exports through a module namespace object.import typeexplicitly marks type-only imports.- Re-exports provide a central entry point for related members.
- Dynamic
import()loads a module asynchronously. - Organize files by responsibility to make projects easier to maintain.