{"componentChunkName":"component---src-templates-documentation-tsx","path":"/docs/handbook/declaration-files/templates/module-d-ts.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/nightly-builds.html"},{"path":"/docs/handbook/declaration-files/by-example.html"},{"path":"/docs/handbook/declaration-files/consumption.html"},{"path":"/docs/handbook/declaration-files/deep-dive.html"},{"path":"/docs/handbook/declaration-files/do-s-and-don-ts.html"},{"path":"/docs/handbook/declaration-files/introduction.html"},{"path":"/docs/handbook/declaration-files/library-structures.html"},{"path":"/docs/handbook/declaration-files/publishing.html"},{"path":"/docs/handbook/declaration-files/templates.html"},{"path":"/docs/handbook/typescript-in-5-minutes-func.html"},{"path":"/docs/handbook/typescript-in-5-minutes.html"},{"path":"/docs/handbook/typescript-in-5-minutes-oop.html"},{"path":"/docs/handbook/typescript-from-scratch.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/intro.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/declaration-files/dts-from-js.html"},{"path":"/docs/handbook/intro-to-js-ts.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/mixins.html"},{"path":"/docs/handbook/module-resolution.html"},{"path":"/docs/handbook/modules.html"},{"path":"/docs/handbook/namespaces-and-modules.html"},{"path":"/docs/handbook/namespaces.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/release-notes/typescript-1-1.html"},{"path":"/docs/handbook/release-notes/typescript-1-3.html"},{"path":"/docs/handbook/release-notes/typescript-1-4.html"},{"path":"/docs/handbook/release-notes/typescript-1-5.html"},{"path":"/docs/handbook/release-notes/typescript-1-6.html"},{"path":"/docs/handbook/release-notes/typescript-1-7.html"},{"path":"/docs/handbook/release-notes/typescript-1-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-0.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-3.html"},{"path":"/docs/handbook/release-notes/typescript-2-4.html"},{"path":"/docs/handbook/release-notes/typescript-2-5.html"},{"path":"/docs/handbook/release-notes/typescript-2-6.html"},{"path":"/docs/handbook/release-notes/typescript-2-7.html"},{"path":"/docs/handbook/release-notes/typescript-2-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-9.html"},{"path":"/docs/handbook/release-notes/typescript-3-0.html"},{"path":"/docs/handbook/release-notes/typescript-3-1.html"},{"path":"/docs/handbook/release-notes/typescript-3-2.html"},{"path":"/docs/handbook/release-notes/typescript-3-3.html"},{"path":"/docs/handbook/release-notes/typescript-3-4.html"},{"path":"/docs/handbook/release-notes/typescript-3-5.html"},{"path":"/docs/handbook/release-notes/typescript-3-6.html"},{"path":"/docs/handbook/release-notes/typescript-3-7.html"},{"path":"/docs/handbook/release-notes/typescript-3-8.html"},{"path":"/docs/handbook/release-notes/typescript-3-9.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/babel-with-typescript.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/vo/docs/handbook/basic-types.html"},{"path":"/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-class-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-function-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-d-ts.html"},{"path":"/docs/handbook/release-notes/overview.html"},{"path":"/tsconfig"},{"path":"/en/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/pt/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/ja/play"},{"path":"/pt/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/fa/play"},{"path":"/play/3-7/fixits/big-number-literals.ts"},{"path":"/play/3-7/fixits/const-to-let.ts"},{"path":"/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/play/3-7/types-and-code-flow/assertion-functions.ts"},{"path":"/play/3-7/types-and-code-flow/recursive-type-references.ts"},{"path":"/play/3-7/types-and-code-flow/uncalled-function-checks.ts"},{"path":"/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/play/3-8/jsdoc-improvements/accessibility-modifiers.js"},{"path":"/play/3-8/syntax-and-messaging/export-modules-from.ts"},{"path":"/play/3-8/syntax-and-messaging/private-class-fields.ts"},{"path":"/play/4-0/new-checks/class-constructor-code-flow.ts"},{"path":"/play/4-0/new-js-features/jsdoc-deprecated.ts"},{"path":"/play/4-0/new-js-features/logical-operators-and-assignment.ts"},{"path":"/play/4-0/new-js-features/nullish-coalescing.ts"},{"path":"/play/4-0/new-ts-features/named-tuples.ts"},{"path":"/play/4-0/new-ts-features/unknown-in-catch.ts"},{"path":"/play/4-0/new-ts-features/variadic-tuples.ts"},{"path":"/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/play/javascript/external-apis/typescript-with-node.js"},{"path":"/play/javascript/external-apis/typescript-with-web.js"},{"path":"/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/play/javascript/helping-with-javascript/errors.ts"},{"path":"/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/play/javascript/javascript-essentials/functions.ts"},{"path":"/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/play/javascript/modern-javascript/async-await.ts"},{"path":"/play/javascript/modern-javascript/immutability.ts"},{"path":"/play/javascript/modern-javascript/import-export.ts"},{"path":"/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/play/javascript/working-with-classes/classes-101.ts"},{"path":"/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/play/javascript/working-with-classes/mixins.ts"},{"path":"/play/javascript/working-with-classes/this.ts"},{"path":"/play/playground/config/javascript-playgrounds.js"},{"path":"/play/playground/config/new-compiler-defaults.ts"},{"path":"/play/playground/language/automatic-type-acquisition.ts"},{"path":"/play/playground/language/fixits.ts"},{"path":"/play/playground/tooling/mobile-support.ts"},{"path":"/play/playground/tooling/sharable-urls.ts"},{"path":"/play/playground/tooling/typescript-versions.ts"},{"path":"/play/typescript/language/soundness.ts"},{"path":"/play/typescript/language/structural-typing.ts"},{"path":"/play/typescript/language/type-guards.ts"},{"path":"/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/play/typescript/language-extensions/enums.ts"},{"path":"/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/play/typescript/meta-types/conditional-types.ts"},{"path":"/play/typescript/meta-types/discriminate-types.ts"},{"path":"/play/typescript/meta-types/indexed-types.ts"},{"path":"/play/typescript/meta-types/mapped-types.ts"},{"path":"/play/typescript/primitives/any.ts"},{"path":"/play/typescript/primitives/literals.ts"},{"path":"/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/play/typescript/primitives/unknown-and-never.ts"},{"path":"/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/play/typescript/type-primitives/nullable-types.ts"},{"path":"/play/typescript/type-primitives/tuples.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-node.js"},{"path":"/es/play/javascript/external-apis/typescript-with-web.js"},{"path":"/es/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/es/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/es/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/es/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/es/play/typescript/language/soundness.ts"},{"path":"/es/play/typescript/language/structural-typing.ts"},{"path":"/es/play/typescript/language/type-guards.ts"},{"path":"/es/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/es/play/typescript/meta-types/conditional-types.ts"},{"path":"/es/play/typescript/meta-types/discriminate-types.ts"},{"path":"/es/play/typescript/meta-types/indexed-types.ts"},{"path":"/es/play/typescript/meta-types/mapped-types.ts"},{"path":"/es/play/typescript/primitives/any.ts"},{"path":"/es/play/typescript/primitives/literals.ts"},{"path":"/es/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/es/play/typescript/primitives/unknown-and-never.ts"},{"path":"/es/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/es/play/typescript/type-primitives/nullable-types.ts"},{"path":"/es/play/typescript/type-primitives/tuples.ts"},{"path":"/fa/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/ja/play/3-7/fixits/big-number-literals.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/ja/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/ja/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/ja/play/typescript/language-extensions/enums.ts"},{"path":"/ja/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/ja/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/ja/play/typescript/primitives/any.ts"},{"path":"/ja/play/typescript/primitives/literals.ts"},{"path":"/ja/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/ja/play/typescript/primitives/unknown-and-never.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/ja/play/typescript/type-primitives/nullable-types.ts"},{"path":"/ja/play/typescript/type-primitives/tuples.ts"},{"path":"/pt/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/pt/play/javascript/javascript-essentials/functions.ts"},{"path":"/pt/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/pt/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/vo/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/vo/play/javascript/javascript-essentials/functions.ts"},{"path":"/vo/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/vo/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-node.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-web.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/zh/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/zh/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/zh/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/zh/play/javascript/helping-with-javascript/errors.ts"},{"path":"/zh/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/zh/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/zh/play/javascript/javascript-essentials/functions.ts"},{"path":"/zh/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/import-export.ts"},{"path":"/zh/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/zh/play/javascript/working-with-classes/classes-101.ts"},{"path":"/zh/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/zh/play/javascript/working-with-classes/mixins.ts"},{"path":"/zh/play/javascript/working-with-classes/this.ts"},{"path":"/zh/play/playground/config/javascript-playgrounds.js"},{"path":"/zh/play/playground/config/new-compiler-defaults.ts"},{"path":"/zh/play/playground/language/automatic-type-acquisition.ts"},{"path":"/zh/play/playground/language/fixits.ts"},{"path":"/zh/play/playground/tooling/mobile-support.ts"},{"path":"/zh/play/playground/tooling/sharable-urls.ts"},{"path":"/zh/play/playground/tooling/typescript-versions.ts"},{"path":"/zh/play/typescript/language/soundness.ts"},{"path":"/zh/play/typescript/language/structural-typing.ts"},{"path":"/zh/play/typescript/language/type-guards.ts"},{"path":"/zh/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/zh/play/typescript/language-extensions/enums.ts"},{"path":"/zh/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/zh/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/zh/play/typescript/meta-types/conditional-types.ts"},{"path":"/zh/play/typescript/meta-types/discriminate-types.ts"},{"path":"/zh/play/typescript/meta-types/indexed-types.ts"},{"path":"/zh/play/typescript/meta-types/mapped-types.ts"},{"path":"/zh/play/typescript/primitives/any.ts"},{"path":"/zh/play/typescript/primitives/literals.ts"},{"path":"/zh/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/zh/play/typescript/primitives/unknown-and-never.ts"},{"path":"/zh/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/zh/play/typescript/type-primitives/nullable-types.ts"},{"path":"/zh/play/typescript/type-primitives/tuples.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/ja/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/ja/play/3-7/fixits/const-to-let.ts"},{"path":"/community"},{"path":"/es/community"},{"path":"/ja/community"},{"path":"/vo/community"},{"path":"/zh/community"},{"path":"/download"},{"path":"/es/download"},{"path":"/ja/download"},{"path":"/vo/download"},{"path":"/zh/download"},{"path":"/empty"},{"path":"/es/empty"},{"path":"/ja/empty"},{"path":"/vo/empty"},{"path":"/zh/empty"},{"path":"/"},{"path":"/es/"},{"path":"/ja/"},{"path":"/vo/"},{"path":"/zh/"},{"path":"/tools"},{"path":"/es/tools"},{"path":"/ja/tools"},{"path":"/vo/tools"},{"path":"/zh/tools"},{"path":"/why-create-typescript"},{"path":"/es/why-create-typescript"},{"path":"/ja/why-create-typescript"},{"path":"/vo/why-create-typescript"},{"path":"/zh/why-create-typescript"},{"path":"/docs/bootstrap"},{"path":"/es/docs/bootstrap"},{"path":"/ja/docs/bootstrap"},{"path":"/vo/docs/bootstrap"},{"path":"/zh/docs/bootstrap"},{"path":"/docs/"},{"path":"/es/docs/"},{"path":"/ja/docs/"},{"path":"/vo/docs/"},{"path":"/zh/docs/"},{"path":"/docs/handbook/"},{"path":"/es/docs/handbook/"},{"path":"/ja/docs/handbook/"},{"path":"/vo/docs/handbook/"},{"path":"/zh/docs/handbook/"},{"path":"/branding/"},{"path":"/dev/bug-workbench/"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"},{"path":"/upcoming/"}]},"markdownRemark":{"id":"e70a3aeb-3fc5-5d49-9db7-5a86eb784698","excerpt":"Comparing JavaScript to an example DTS Common CommonJS Patterns A module using CommonJS patterns uses module.exports to describe the exported values. For…","html":"<h2 id=\"comparing-javascript-to-an-example-dts\" style=\"position:relative;\"><a href=\"#comparing-javascript-to-an-example-dts\" aria-label=\"comparing javascript to an example dts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Comparing JavaScript to an example DTS</h2>\n<h2 id=\"common-commonjs-patterns\" style=\"position:relative;\"><a href=\"#common-commonjs-patterns\" aria-label=\"common commonjs patterns permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Common CommonJS Patterns</h2>\n<p>A module using CommonJS patterns uses <code>module.exports</code> to describe the exported values. For example, here is a module which exports a function and a numerical constant:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">maxInterval</span><span style=\"color: #000000\"> = </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">slice</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">module.exports = {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">getArrayLength</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">maxInterval</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>This can be described by the following <code>.d.ts</code>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">: any[]): number;</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">maxInterval</span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>The TypeScript playground can show you the <code>.d.ts</code> equivalent for JavaScript code. You can <a href=\"/play?useJavaScript=true#code/GYVwdgxgLglg9mABAcwKZQIICcsEMCeAMqmMlABYAUuOAlIgN6IBQiiW6IWSNWAdABsSZcswC+zCAgDOURAFtcADwAq5GKUQBeRAEYATM2by4AExBC+qJQAc4WKNO2NWKdNjxFhFADSvFquqk4sxAA\">try it yourself here</a>.</p>\n<p>The <code>.d.ts</code> syntax intentionally looks like <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import\">ES Modules</a> syntax.\nES Modules was ratified by TC39 in 2019, while it has been available via transpilers for a long time, however if you have a JavaScript codebase using ES Modules:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">.length;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>This would have the following <code>.d.ts</code> equivalent:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">: any[]): number;</span></code></div></pre>\n<h3 id=\"default-exports\" style=\"position:relative;\"><a href=\"#default-exports\" aria-label=\"default exports permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Default Exports</h3>\n<p>In CommonJS you can export any value as the default export, for example here is a regular expression module:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">module.exports =</span><span style=\"color: #A31515\"> /hello( world)</span><span style=\"color: #000000\">?</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Which can be described by the following .d.ts:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">helloWorld</span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> RegExp</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">default</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">helloWorld</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Or a number:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">module.exports = </span><span style=\"color: #09835A\">3.142</span><span style=\"color: #000000\">;</span></code></div></pre>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">pi</span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> number</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">default</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">pi</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>One style of exporting in CommonJS is to export a function.\nBecause a function is also an object, then extra fields can be added are included in the export.</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">slice</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #1A1A1A\">getArrayLength</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">maxInterval</span><span style=\"color: #000000\"> = </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #000000\">module.exports = </span><span style=\"color: #1A1A1A\">getArrayLength</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Which can be described with:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">default</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">: any[]): number;</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">maxInterval</span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Note that using <code>export default</code> in your .d.ts files requires <a href=\"/tsconfig#esModuleInterop\"><code>esModuleInterop: true</code></a> to work.\nIf you can’t have <code>esModuleInterop: true</code> in your project, such as when you’re submitting a PR to Definitely Typed, you’ll have to use the <code>export=</code> syntax instead. This older syntax is harder to use but works everywhere.\nHere’s how the above example would have to be written using <code>export=</code>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">: any[]): number;</span>\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> getArrayLength {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">maxInterval</span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> = </span><span style=\"color: #1A1A1A\">getArrayLength</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>See <a href=\"/5bc00d38ab2cd134cf9690e7a4343bf5/module-function.d.ts.md\">Module: Functions</a> for details of how that works, and the <a href=\"/docs/handbook/modules.html\">Modules reference</a> page.</p>\n<h2 id=\"handling-many-consuming-import\" style=\"position:relative;\"><a href=\"#handling-many-consuming-import\" aria-label=\"handling many consuming import permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Handling Many Consuming Import</h2>\n<p>There are many ways to import a module in modern consuming code:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">fastify </span><span style=\"color: #000000\">= require(</span><span style=\"color: #A31515\">\"fastify\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> { </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\"> } = require(</span><span style=\"color: #A31515\">\"fastify\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\"> = </span><span style=\"color: #0000FF\">require</span><span style=\"color: #000000\">(</span><span style=\"color: #A31515\">\"fastify\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">Fastify</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"fastify\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">, </span><span style=\"color: #1A1A1A\">FastifyInstance</span><span style=\"color: #000000\"> } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"fastify\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"fastify\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">, { </span><span style=\"color: #1A1A1A\">FastifyInstance</span><span style=\"color: #000000\"> } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"fastify\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Covering all of these cases requires the JavaScript code to actually support all of these patterns.\nTo support many of these patterns, a CommonJS module would need to look something like:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> FastifyInstance {}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> fastify() {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> FastifyInstance();</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">FastifyInstance</span><span style=\"color: #000000\"> = </span><span style=\"color: #1A1A1A\">FastifyInstance</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #008000\">// Allows for { fastify }</span>\n<span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\"> = </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #008000\">// Allows for strict ES Module support</span>\n<span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">default</span><span style=\"color: #000000\"> = </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #008000\">// Sets the default export</span>\n<span style=\"color: #000000\">module.exports = </span><span style=\"color: #1A1A1A\">fastify</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h2 id=\"types-in-modules\" style=\"position:relative;\"><a href=\"#types-in-modules\" aria-label=\"types in modules permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Types in Modules</h2>\n<p>You may want to provide a type for JavaScript code which does not exist</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayMetadata(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">    length: getArrayLength(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">),</span>\n<span style=\"color: #000000\">    firstObject: </span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">[</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">],</span>\n<span style=\"color: #000000\">  };</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">module.exports = {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">getArrayMetadata</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>This can be described with:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> ArrayMetadata = {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">length</span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">firstObject</span><span style=\"color: #000000\">: any | undefined;</span>\n<span style=\"color: #000000\">};</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayMetadata(</span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">: any[]): ArrayMetadata;</span></code></div></pre>\n<p>This example is a good case for <a href=\"/docs/handbook/generics.html#generic-types\">using generics</a> to provide richer type information:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> ArrayMetadata&lt;ArrType&gt; = {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">length</span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">firstObject</span><span style=\"color: #000000\">: ArrType | undefined;</span>\n<span style=\"color: #000000\">};</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getArrayMetadata&lt;ArrType&gt;(</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">arr</span><span style=\"color: #000000\">: ArrType[]</span>\n<span style=\"color: #000000\">): ArrayMetadata&lt;ArrType&gt;;</span></code></div></pre>\n<p>Now the type of the array propagates into the <code>ArrayMetadata</code> type.</p>\n<p>The types which are exported can then be re-used by consumers of the modules using either <code>import</code> or <code>import type</code> in TypeScript code or <a href=\"/docs/handbook/jsdoc-supported-types.html#import-types\">JSDoc imports</a>.</p>\n<h3 id=\"namespaces-in-module-code\" style=\"position:relative;\"><a href=\"#namespaces-in-module-code\" aria-label=\"namespaces in module code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Namespaces in Module Code</h3>\n<p>Trying to describe the runtime relationship of JavaScript code can be tricky.\nWhen the ES Module-like syntax doesn’t provide enough tools to describe the exports then you can use <code>namespaces</code>.</p>\n<p>For example, you may have complex enough types to describe that you choose to namespace them inside your <code>.d.ts</code>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// This represents the JavaScript class which would be available at runtime</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> API {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #1A1A1A\">baseURL</span><span style=\"color: #000000\">: string);</span>\n<span style=\"color: #000000\">  getInfo(</span><span style=\"color: #1A1A1A\">opts</span><span style=\"color: #000000\">: API.InfoRequest): API.InfoResponse;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// This namespace is merged with the API class and allows for consumers, and this file</span>\n<span style=\"color: #008000\">// to have types which are nested away in their own sections.</span>\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> API {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> InfoRequest {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #1A1A1A\">id</span><span style=\"color: #000000\">: string;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> InfoResponse {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #1A1A1A\">width</span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #1A1A1A\">height</span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>To understand how namespaces work in <code>.d.ts</code> files read the <a href=\"/docs/handbook/declaration-files/deep-dive.html\"><code>.d.ts</code> deep dive</a>.</p>\n<h3 id=\"optional-global-usage\" style=\"position:relative;\"><a href=\"#optional-global-usage\" aria-label=\"optional global usage permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Optional Global Usage</h3>\n<p>You can use <code>export as namespace</code> to declare that your module will be available in the global scope in UMD contexts:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> moduleName;</span></code></div></pre>\n<h2 id=\"reference-example\" style=\"position:relative;\"><a href=\"#reference-example\" aria-label=\"reference example permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reference Example</h2>\n<p>To give you an idea of how all these pieces can come together, here is a reference <code>.d.ts</code> to start with when making a new module</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]</span>\n<span style=\"color: #008000\">// Project: [~THE PROJECT NAME~]</span>\n<span style=\"color: #008000\">// Definitions by: [~YOUR NAME~] &lt;[~A URL FOR YOU~]&gt;</span>\n\n<span style=\"color: #008000\">/*~ This is the module template file. You should rename it to index.d.ts</span>\n<span style=\"color: #008000\"> *~ and place it in a folder with the same name as the module.</span>\n<span style=\"color: #008000\"> *~ For example, if you were writing a file for \"super-greeter\", this</span>\n<span style=\"color: #008000\"> *~ file should be 'super-greeter/index.d.ts'</span>\n<span style=\"color: #008000\"> */</span>\n\n<span style=\"color: #008000\">/*~ If this module is a UMD module that exposes a global variable 'myLib' when</span>\n<span style=\"color: #008000\"> *~ loaded outside a module loader environment, declare that global here.</span>\n<span style=\"color: #008000\"> *~ Otherwise, delete this declaration.</span>\n<span style=\"color: #008000\"> */</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> myLib;</span>\n\n<span style=\"color: #008000\">/*~ If this module exports functions, declare them like so.</span>\n<span style=\"color: #008000\"> */</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> myFunction(</span><span style=\"color: #1A1A1A\">a</span><span style=\"color: #000000\">: string): string;</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> myOtherFunction(</span><span style=\"color: #1A1A1A\">a</span><span style=\"color: #000000\">: number): number;</span>\n\n<span style=\"color: #008000\">/*~ You can declare types that are available via importing the module */</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> SomeType {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">name</span><span style=\"color: #000000\">: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">length</span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\">extras</span><span style=\"color: #000000\">?: string[];</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">/*~ You can declare properties of the module using const, let, or var */</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">myField</span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> number</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h3 id=\"library-file-layout\" style=\"position:relative;\"><a href=\"#library-file-layout\" aria-label=\"library file layout permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Library file layout</h3>\n<p>The layout of your declaration files should mirror the layout of the library.</p>\n<p>A library can consist of multiple modules, such as</p>\n<pre><code>myLib\n  +---- index.js\n  +---- foo.js\n  +---- bar\n         +---- index.js\n         +---- baz.js\n</code></pre>\n<p>These could be imported as</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">a</span><span style=\"color: #000000\"> = require(</span><span style=\"color: #A31515\">\"myLib\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">b</span><span style=\"color: #000000\"> = require(</span><span style=\"color: #A31515\">\"myLib/foo\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">c</span><span style=\"color: #000000\"> = require(</span><span style=\"color: #A31515\">\"myLib/bar\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">d</span><span style=\"color: #000000\"> = require(</span><span style=\"color: #A31515\">\"myLib/bar/baz\"</span><span style=\"color: #000000\">);</span></code></div></pre>\n<p>Your declaration files should thus be</p>\n<pre><code>@types/myLib\n  +---- index.d.ts\n  +---- foo.d.ts\n  +---- bar\n         +---- index.d.ts\n         +---- baz.d.ts\n</code></pre>\n<h3 id=\"testing-your-types\" style=\"position:relative;\"><a href=\"#testing-your-types\" aria-label=\"testing your types permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Testing your types</h3>\n<p>If you are planning on submitting these changes to DefinitelyTyped for everyone to also use, then we recommend you:</p>\n<blockquote>\n<ol>\n<li>Create a new folder in <code>node_modules/@types/[libname]</code></li>\n<li>Create an <code>index.d.ts</code> in that folder, and copy the example in</li>\n<li>See where your usage of the module breaks, and start to fill out the index.d.ts</li>\n<li>When you’re happy, clone <a href=\"https://github.com/DefinitelyTyped\">DefinitelyTyped/DefinitelyTyped</a> and follow the instructions in the README. </li>\n</ol>\n</blockquote>\n<p>Otherwise</p>\n<blockquote>\n<ol>\n<li>Create a new file in the root of your source tree: <code>[libname].d.ts</code></li>\n<li>Add <code>declare module \"[libname]\" { }</code></li>\n<li>Add the template inside the braces of the declare module, and see where your usage breaks </li>\n</ol>\n</blockquote>","headings":[{"value":"Comparing JavaScript to an example DTS","depth":2},{"value":"Common CommonJS Patterns","depth":2},{"value":"Default Exports","depth":3},{"value":"Handling Many Consuming Import","depth":2},{"value":"Types in Modules","depth":2},{"value":"Namespaces in Module Code","depth":3},{"value":"Optional Global Usage","depth":3},{"value":"Reference Example","depth":2},{"value":"Library file layout","depth":3},{"value":"Testing your types","depth":3}],"frontmatter":{"permalink":"/docs/handbook/declaration-files/templates/module-d-ts.html","title":"Modules .d.ts","disable_toc":null,"oneline":null}},"prev":null,"next":null},"pageContext":{"id":"modules-.d.ts","slug":"/docs/handbook/declaration-files/templates/module-d-ts.html","repoPath":"/packages/documentation/copy/en/declaration-files/templates/module.d.ts.md","lang":"en","modifiedTime":"2020-08-07T17:00:36.335Z"}}}