What are modules in TypeScript?
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.
Previous
What is the difference between Partial and Required utility types?
Next
What is declaration merging in TypeScript?