The shape of a declaration file follows how the JavaScript is loaded, so I first read the library's code. The handbook maps library kinds to templates (global.d.ts, module.d.ts, module-function.d.ts, module-class.d.ts, plus plugin variants).
- Global library (a
<script>that assignswindow.myLib). The.d.tsis a script: no top-levelimport/export, justdeclare function myLib(...),declare namespace myLib { ... }and interfaces, all of which become global. The handbook warns that most "global" libraries are really UMD, so check fordefine/module.exportsfirst. - Module whose export is one function or class (
module.exports = fn). An ES module can't express "the module is this function", so writeexport = fn. To export types too, declare anamespacewith the same name: it merges with the function, sofn.Optionsis a type andfn.versiona property. Consumers write a default import (withesModuleInterop: 6.0.3 and 7.0.2 emitted__importDefaultwithout the flag) orimport fn = require(...). - UMD library (works as a module and as a global). Write the module form and add
export as namespace myLib;. Script files (no imports) can then usemyLibdirectly; module files must import it, ortscreports TS2686 unlessallowUmdGlobalAccessis on. - Plain ES-style module: named
export declarestatements (themodule.d.tstemplate).
Per-version types. typesVersions in package.json maps a semver range of the TypeScript version to other files, first match wins, falling back to types. When exports is read, the modules reference says typesVersions is ignored; use versioned conditions such as "types@>=5.2" there instead.