Appendix D. Recipes for Writing Declaration Files for Third-Party JavaScript Modules
This appendix covers a few key building blocks and patterns that come up over and over again when typing third-party modules. For a deeper discussion of typing third-party code, head over to “JavaScript That Doesn’t Have Type Declarations on DefinitelyTyped”.
Since module declaration files have to live in .d.ts files and so can’t contain values, when you declare module types you need to use the declare keyword to affirm that values of the given type really are exported by your module. Table D-1 provides a short summary of regular declarations and their type declaration equivalents.
| .ts | .d.ts |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Types of Exports
Whether your module uses global, ES2015, or CommonJS exports will affect how you write your declaration files.
Globals
If your module only assigns values to the global namespace and doesn’t actually export anything, you can just create a script-mode file (see “Module Mode Versus Script Mode”) and prefix your variable, function, and class declarations with declare
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access