How do you type a CommonJS module using 'export =' and 'import ... = require(...)'?
Short answer
'export =' in a .d.ts describes a module whose entire export is a single value (common in older CommonJS libraries), rather than a set of named exports — consumers then use 'import x = require("module")' syntax (or esModuleInterop-based default import) to bring it in correctly.
Advertisement
AModel answer
'export =' in a .d.ts describes a module whose entire export is a single value (common in older CommonJS libraries), rather than a set of named exports — consumers then use 'import x = require("module")' syntax (or esModuleInterop-based default import) to bring it in correctly.
// legacy-lib.d.ts
declare function legacyLib(config: object): void;
export = legacyLib;
// usage.ts
import legacyLib = require('legacy-lib');Real-world example
Typing an older CommonJS npm package whose module.exports is a single function rather than an object of named exports.
Likely follow-ups
- How does the esModuleInterop compiler option change how you can import an 'export =' module?
Related topics