Lesson 23 / 26
Modules: import & export
Split code across files and share types and values between them.
Named exports
A file can export as many named values, functions, types or interfaces as it needs.
// math.ts
export function add(a: number, b: number): number {
return a + b;
}
export interface Point {
x: number;
y: number;
}Default export & import
A file has at most one export default — imported without curly braces, and can be renamed freely.
// logger.ts
export default function log(msg: string) {
console.log(`[LOG] ${msg}`);
}
// app.ts
import myLogger from "./logger";
import { add, Point } from "./math";
myLogger("started");Type-only imports
import type { Point } from "./math" imports only the type, which is erased entirely at compile time — nothing extra ships in the output JS.
Quick check: How many `export default` statements can one file have?
- At most one
- Unlimited
- Exactly one per interface
Answer
At most one — A module can have many named exports but only a single default export.