What are modules in TypeScript?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for TypeScript development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

TypeScript uses JavaScript's module system with enhanced type support. A file is a module if it has at least one top-level import or export statement; otherwise it is a script (global scope). Exports: export const PI = 3.14;, export function greet() {}, export interface User {}, export default class App {}. Imports: import { PI, greet } from "./utils";, import App from "./App";, import * as Utils from "./utils";. TypeScript also supports type-only imports/exports (TS 3.8+): import type { User } from "./types"; — these are completely erased at compile time, which helps bundlers optimize tree-shaking. The module option in tsconfig controls the module format (commonjs, es2020, node16, etc.). TypeScript understands declaration files (.d.ts) that provide type information for JavaScript packages without rewriting them.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a TypeScript codebase.