export {} to Force Module Context
What you'll learn: How to prevent a .d.ts file from polluting the global scope by forcing it into module mode with an empty export.
The Problem: Accidental Global Scope Pollution
By default, a declaration file (.d.ts) that contains no imports or exports is treated as a script file. This means every type and declaration you write becomes global — available everywhere in your project without importing.
While this might sound convenient, it's usually undesirable. Global types can cause naming conflicts, make dependencies unclear, and violate the principle of explicit imports.
The Solution: Add export {}
To force TypeScript to treat your .d.ts file as a module (not a script), add this single line:
export {};
This empty export statement has no runtime effect and exports nothing, but it signals to TypeScript: "This file is a module, not a global script file."
Once your file is a module, all types declared inside are local to that file unless explicitly exported. Other files must import them to use them.
Analogy: Posting on a Public Board vs Sending a Letter
Think of a script-mode .d.ts file like posting announcements on a public bulletin board — everyone sees them automatically. The export {} trick is like switching to a mailbox system: you must explicitly address and send (export) messages, and others must deliberately check their mail (import) to receive them.
When to Use This Pattern
- Module-scoped augmentations: When using
declare module 'some-package'to add types to a third-party library, addexport {}to keep those augmentations modular. - Preventing global leakage: When your
.d.tsfile contains helper types you want to export selectively. - Following best practices: Most modern TypeScript projects prefer explicit imports over globals.
Key Takeaway: Adding export {} to a .d.ts file converts it from global script mode to module mode, preventing unintentional global scope pollution while keeping type declarations properly scoped.