Modules
A module is just a file — anything you export from it becomes available to other files that import it. TypeScript's module syntax is the same ES module syntax modern JavaScript already uses; the compiler adds type checking across those import/export boundaries on top.
Named exports
export function add(a: number, b: number): number { return a + b; } export const PI = 3.14159;
import { add, PI } from "./math"; console.log(add(2, 3)); console.log(PI);
5 3.14159
Each name has to be exported explicitly with export, and imported with the exact same name in curly braces — import { add } pulls in only add, not everything the file exports.
Default exports
class Logger {
log(message: string) {
console.log(`[LOG] ${message}`);
}
}
export default Logger;
import Logger from "./logger"; const logger = new Logger(); logger.log("Server started");
[LOG] Server started
A file can have at most one export default — the importing file can then give it whatever local name it wants (Logger here, but it isn't required to match), unlike named exports which must be imported by their exact declared name.
Importing types
import type { User } from "./user"; function greet(user: User) { console.log(`Hi, ${user.name}`); }
import type makes it explicit that only the type is being imported, not any runtime value — the compiler strips it out entirely from the compiled JavaScript, since types don't exist once compilation is done.
module and moduleResolution settings in tsconfig.json control. Mismatched settings between your tsconfig.json and your actual runtime (say, targeting CommonJS while running in a browser expecting ES modules) is one of the most common sources of confusing "works when compiled, breaks when run" errors — covered next.