Sub topic 1
Scripts versus modules: what makes a file a module
TypeScript treats each source file as either a script or a module. This distinction controls whether declarations are private to the file or visible in the global scope.
A script is a file without a top-level import, export, or await statement. Its declarations belong to the global scope. A module is a file with at least one top-level import or export statement. Its declarations belong to that file's module scope.
Prerequisites
You should know basic TypeScript types, interfaces, and functions. You should also be familiar with JavaScript import and export statements, tsc, tsconfig.json, and basic package.json usage.
How TypeScript identifies a module
Consider this file:
// settings.ts
const apiUrl = "https://example.com";
function getApiUrl(): string {
return apiUrl;
}This file is a script. Although apiUrl and getApiUrl are not exported, they can conflict with declarations from another script in the same compilation.
Add an export:
// settings.ts
export const apiUrl = "https://example.com";
export function getApiUrl(): string {
return apiUrl;
}The file is now a module. Its declarations are private by default. Other files can use them only through an explicit import:
// app.ts
import { getApiUrl } from "./settings.js";
console.log(getApiUrl());The .js extension is intentional when compiling for Node.js ECMAScript modules. TypeScript checks the .ts source file but preserves the import for the emitted .js file.
Why the distinction matters
Module scope prevents unrelated files from accidentally sharing names. It also makes dependencies visible.
| File kind | Scope of top-level declarations | Imports and exports |
|---|---|---|
| Script | Global scope | None required |
| Module | Private module scope | At least one top-level import or export |
A script can accidentally create a global name:
// first.ts
const version = "one";
// second.ts
const version = "two"; // Error: the name may already existMaking both files modules isolates their declarations:
// first.ts
export const version = "one";
// second.ts
export const version = "two";The exports do not make the values globally available. A consumer must import the desired value.
Making a file a module without exporting an API
Sometimes a file should run setup code but expose nothing. Add an empty export:
// startup.ts
initializeLogging();
function initializeLogging(): void {
console.log("Logging is ready");
}
export {};The export {} statement marks the file as a module. The function remains private, while the module's top-level code still runs when another module imports it:
import "./startup.js";This pattern is useful for initialization, polyfills, and global configuration. Use it carefully because imports with side effects can make execution order harder to understand.
Seeing the module boundary
The compiler can also detect scripts that are not modules when the same global name appears more than once. This is often the source of confusing duplicate-identifier errors.
Exercise
What single change makes a file a module if it has no imports or public exports?
Show answer
Add export {} at the top or bottom of the file. This creates a module boundary without exporting a value.