Modules
Export and import names
Use ES module syntax. Compile with a module setting that matches your runtime.
// math.ts
export function square(n: number): number {
return n * n;
}
// app.ts
import { square } from "./math";
console.log(square(4));
Default exports
A file may have one default export. Named exports are easier to rename safely.
// greeter.ts
export default function greet(name: string) {
return `Hi, ${name}`;
}
// app.ts
import greet from "./greeter";
console.log(greet("TypeScript"));
Compile multiple files
With a tsconfig.json, run tsc once for the whole project.
tsc
node dist/app.js
Module habits
- Keep one clear idea per file
- Prefer named exports for libraries and shared utilities
- Match
modulein tsconfig to Node ESM/CJS or bundlers - Avoid circular imports between modules
Comments
One comment per signed-in account. Comments are saved with this page’s URL.