{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/modules.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/literal-types.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/nightly-builds.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/writing-declaration-files.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/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-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.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/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react-&-webpack.html"},{"path":"/docs/handbook/react.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/typescript-tooling-in-5-minutes.html"},{"path":"/docs/handbook/tsconfig-json.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":"/ja/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/ja/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/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/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":"/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/type-primitives/built-in-utility-types.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/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":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.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/home"},{"path":"/es/docs/home"},{"path":"/ja/docs/home"},{"path":"/vo/docs/home"},{"path":"/zh/docs/home"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"}]},"markdownRemark":{"id":"df82b75e-697e-5238-8c37-897b315260b8","excerpt":"Introduction Starting with ECMAScript 2015, JavaScript has a concept of modules. TypeScript shares this concept. Modules are executed within their own scope…","html":"<h1 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction 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>Introduction</h1>\n<p>Starting with ECMAScript 2015, JavaScript has a concept of modules. TypeScript shares this concept.</p>\n<p>Modules are executed within their own scope, not in the global scope; this means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the <a href=\"#export\"><code>export</code> forms</a>.\nConversely, to consume a variable, function, class, interface, etc. exported from a different module, it has to be imported using one of the <a href=\"#import\"><code>import</code> forms</a>.</p>\n<p>Modules are declarative; the relationships between modules are specified in terms of imports and exports at the file level.</p>\n<p>Modules import one another using a module loader.\nAt runtime the module loader is responsible for locating and executing all dependencies of a module before executing it.\nWell-known module loaders used in JavaScript are Node.js’s loader for <a href=\"https://en.wikipedia.org/wiki/CommonJS\">CommonJS</a> modules and the <a href=\"http://requirejs.org/\">RequireJS</a> loader for <a href=\"https://github.com/amdjs/amdjs-api/blob/master/AMD.md\">AMD</a> modules in Web applications.</p>\n<p>In TypeScript, just as in ECMAScript 2015, any file containing a top-level <code>import</code> or <code>export</code> is considered a module.\nConversely, a file without any top-level <code>import</code> or <code>export</code> declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).</p>\n<h1 id=\"export\" style=\"position:relative;\"><a href=\"#export\" aria-label=\"export 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>Export</h1>\n<h2 id=\"exporting-a-declaration\" style=\"position:relative;\"><a href=\"#exporting-a-declaration\" aria-label=\"exporting a declaration 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>Exporting a declaration</h2>\n<p>Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the <code>export</code> keyword.</p>\n<h5 id=\"stringvalidatorts\" style=\"position:relative;\"><a href=\"#stringvalidatorts\" aria-label=\"stringvalidatorts 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>StringValidator.ts</h5>\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\">interface</span><span style=\"color: #000000\"> StringValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string): boolean;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"zipcodevalidatorts\" style=\"position:relative;\"><a href=\"#zipcodevalidatorts\" aria-label=\"zipcodevalidatorts 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>ZipCodeValidator.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { StringValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./StringValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> numberRegexp =</span><span style=\"color: #A31515\"> /</span><span style=\"color: #0000FF\">^</span><span style=\"color: #A31515\">[0-9]</span><span style=\"color: #000000\">+</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> ZipCodeValidator </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> StringValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> s.length === </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> && numberRegexp.test(s);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h2 id=\"export-statements\" style=\"position:relative;\"><a href=\"#export-statements\" aria-label=\"export statements 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>Export statements</h2>\n<p>Export statements are handy when exports need to be renamed for consumers, so the above example can be written as:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> ZipCodeValidator </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> StringValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> s.length === </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> && numberRegexp.test(s);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> { ZipCodeValidator };</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> { ZipCodeValidator </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> mainValidator };</span></code></div></pre>\n<h2 id=\"re-exports\" style=\"position:relative;\"><a href=\"#re-exports\" aria-label=\"re 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>Re-exports</h2>\n<p>Often modules extend other modules, and partially expose some of their features.\nA re-export does not import it locally, or introduce a local variable.</p>\n<h5 id=\"parseintbasedzipcodevalidatorts\" style=\"position:relative;\"><a href=\"#parseintbasedzipcodevalidatorts\" aria-label=\"parseintbasedzipcodevalidatorts 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>ParseIntBasedZipCodeValidator.ts</h5>\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\">class</span><span style=\"color: #000000\"> ParseIntBasedZipCodeValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> s.length === </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> && parseInt(s).toString() === s;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// Export original validator but rename it</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> { ZipCodeValidator </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> RegExpBasedZipCodeValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Optionally, a module can wrap one or more modules and combine all their exports using <code>export * from \"module\"</code> syntax.</p>\n<h5 id=\"allvalidatorsts\" style=\"position:relative;\"><a href=\"#allvalidatorsts\" aria-label=\"allvalidatorsts 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>AllValidators.ts</h5>\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\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./StringValidator\"</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">// exports 'StringValidator' interface</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">// exports 'ZipCodeValidator' and const 'numberRegexp' class</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ParseIntBasedZipCodeValidator\"</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">//  exports the 'ParseIntBasedZipCodeValidator' class</span>\n<span style=\"color: #008000\">// and re-exports 'RegExpBasedZipCodeValidator' as alias</span>\n<span style=\"color: #008000\">// of the 'ZipCodeValidator' class from 'ZipCodeValidator.ts'</span>\n<span style=\"color: #008000\">// module.</span></code></div></pre>\n<h1 id=\"import\" style=\"position:relative;\"><a href=\"#import\" aria-label=\"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>Import</h1>\n<p>Importing is just about as easy as exporting from a module.\nImporting an exported declaration is done through using one of the <code>import</code> forms below:</p>\n<h2 id=\"import-a-single-export-from-a-module\" style=\"position:relative;\"><a href=\"#import-a-single-export-from-a-module\" aria-label=\"import a single export from a module 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>Import a single export from a module</h2>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { ZipCodeValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myValidator = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> ZipCodeValidator();</span></code></div></pre>\n<p>imports can also be renamed</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { ZipCodeValidator </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> ZCV } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myValidator = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> ZCV();</span></code></div></pre>\n<h2 id=\"import-the-entire-module-into-a-single-variable-and-use-it-to-access-the-module-exports\" style=\"position:relative;\"><a href=\"#import-the-entire-module-into-a-single-variable-and-use-it-to-access-the-module-exports\" aria-label=\"import the entire module into a single variable and use it to access the module 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>Import the entire module into a single variable, and use it to access the module exports</h2>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><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\"> validator </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myValidator = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> validator.ZipCodeValidator();</span></code></div></pre>\n<h2 id=\"import-a-module-for-side-effects-only\" style=\"position:relative;\"><a href=\"#import-a-module-for-side-effects-only\" aria-label=\"import a module for side effects only 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>Import a module for side-effects only</h2>\n<p>Though not recommended practice, some modules set up some global state that can be used by other modules.\nThese modules may not have any exports, or the consumer is not interested in any of their exports.\nTo import these modules, use:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./my-module.js\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h2 id=\"importing-types\" style=\"position:relative;\"><a href=\"#importing-types\" aria-label=\"importing 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>Importing Types</h2>\n<p>Prior to TypeScript 3.8, you can import a type using <code>import</code>.\nWith TypeScript 3.8, you can import a type using the <code>import</code> statement, or using <code>import type</code>.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// Re-using the same import</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> {APIResponseType} </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./api\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #008000\">// Explicitly use import type</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> type {APIResponseType} </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./api\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p><code>import type</code> is always guaranteed to be removed from your JavaScript, and tools like Babel can make better assumptions about your code via the <code>isolatedModules</code> compiler flag.\nYou can read more in the <a href=\"https://devblogs.microsoft.com/typescript/announcing-typescript-3-8-beta/#type-only-imports-exports\">3.8 release notes</a>.</p>\n<h1 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</h1>\n<p>Each module can optionally export a <code>default</code> export.\nDefault exports are marked with the keyword <code>default</code>; and there can only be one <code>default</code> export per module.\n<code>default</code> exports are imported using a different import form.</p>\n<p><code>default</code> exports are really handy.\nFor instance, a library like jQuery might have a default export of <code>jQuery</code> or <code>$</code>, which we’d probably also import under the name <code>$</code> or <code>jQuery</code>.</p>\n<h5 id=\"jquerydts\" style=\"position:relative;\"><a href=\"#jquerydts\" aria-label=\"jquerydts 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><a href=\"https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jquery/JQuery.d.ts\">JQuery.d.ts</a></h5>\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\">let</span><span style=\"color: #000000\"> $: JQuery;</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">default</span><span style=\"color: #000000\"> $;</span></code></div></pre>\n<h5 id=\"appts\" style=\"position:relative;\"><a href=\"#appts\" aria-label=\"appts 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>App.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> $ </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"jquery\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #000000\">$(</span><span style=\"color: #A31515\">\"button.continue\"</span><span style=\"color: #000000\">).html(</span><span style=\"color: #A31515\">\"Next Step...\"</span><span style=\"color: #000000\">);</span></code></div></pre>\n<p>Classes and function declarations can be authored directly as default exports.\nDefault export class and function declaration names are optional.</p>\n<h5 id=\"zipcodevalidatorts-1\" style=\"position:relative;\"><a href=\"#zipcodevalidatorts-1\" aria-label=\"zipcodevalidatorts 1 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>ZipCodeValidator.ts</h5>\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\">class</span><span style=\"color: #000000\"> ZipCodeValidator {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">static</span><span style=\"color: #000000\"> numberRegexp =</span><span style=\"color: #A31515\"> /</span><span style=\"color: #0000FF\">^</span><span style=\"color: #A31515\">[0-9]</span><span style=\"color: #000000\">+</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  isAcceptable(s: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> s.length === </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> && ZipCodeValidator.numberRegexp.test(s);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"testts\" style=\"position:relative;\"><a href=\"#testts\" aria-label=\"testts 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>Test.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> validator </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myValidator = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> validator();</span></code></div></pre>\n<p>or</p>\n<h5 id=\"staticzipcodevalidatorts\" style=\"position:relative;\"><a href=\"#staticzipcodevalidatorts\" aria-label=\"staticzipcodevalidatorts 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>StaticZipCodeValidator.ts</h5>\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\"> numberRegexp =</span><span style=\"color: #A31515\"> /</span><span style=\"color: #0000FF\">^</span><span style=\"color: #A31515\">[0-9]</span><span style=\"color: #000000\">+</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">;</span>\n\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: #0000FF\">function</span><span style=\"color: #000000\">(s: string) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> s.length === </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> && numberRegexp.test(s);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"testts-1\" style=\"position:relative;\"><a href=\"#testts-1\" aria-label=\"testts 1 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>Test.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> validate </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./StaticZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> strings = [</span><span style=\"color: #A31515\">\"Hello\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"98052\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"101\"</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #008000\">// Use function validate</span>\n<span style=\"color: #000000\">strings.forEach(s </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">  console.log(</span><span style=\"color: #A31515\">`\"</span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">s</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">\" </span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">validate(s) </span><span style=\"color: #000000\">?</span><span style=\"color: #A31515\"> \"matches\" </span><span style=\"color: #000000\">:</span><span style=\"color: #A31515\"> \"does not match\"</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">});</span></code></div></pre>\n<p><code>default</code> exports can also be just values:</p>\n<h5 id=\"onetwothreets\" style=\"position:relative;\"><a href=\"#onetwothreets\" aria-label=\"onetwothreets 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>OneTwoThree.ts</h5>\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: #A31515\">\"123\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h5 id=\"logts\" style=\"position:relative;\"><a href=\"#logts\" aria-label=\"logts 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>Log.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> num </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./OneTwoThree\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #000000\">console.log(num); </span><span style=\"color: #008000\">// \"123\"</span></code></div></pre>\n<h2 id=\"export-all-as-x\" style=\"position:relative;\"><a href=\"#export-all-as-x\" aria-label=\"export all as x 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>Export all as x</h2>\n<p>With TypeScript 3.8, you can use <code>export * as ns</code> as a shorthand for re-exporting another module with a name:</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\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> utilities </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./utilities\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>This takes all of the dependencies from a module and makes it an exported field, you could import it like this:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { utilities } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./index\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h1 id=\"export--and-import--require\" style=\"position:relative;\"><a href=\"#export--and-import--require\" aria-label=\"export  and import  require 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><code>export =</code> and <code>import = require()</code></h1>\n<p>Both CommonJS and AMD generally have the concept of an <code>exports</code> object which contains all exports from a module.</p>\n<p>They also support replacing the <code>exports</code> object with a custom single object.\nDefault exports are meant to act as a replacement for this behavior; however, the two are incompatible.\nTypeScript supports <code>export =</code> to model the traditional CommonJS and AMD workflow.</p>\n<p>The <code>export =</code> syntax specifies a single object that is exported from the module.\nThis can be a class, interface, namespace, function, or enum.</p>\n<p>When exporting a module using <code>export =</code>, TypeScript-specific <code>import module = require(\"module\")</code> must be used to import the module.</p>\n<h5 id=\"zipcodevalidatorts-2\" style=\"position:relative;\"><a href=\"#zipcodevalidatorts-2\" aria-label=\"zipcodevalidatorts 2 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>ZipCodeValidator.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> numberRegexp =</span><span style=\"color: #A31515\"> /</span><span style=\"color: #0000FF\">^</span><span style=\"color: #A31515\">[0-9]</span><span style=\"color: #000000\">+</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> ZipCodeValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> s.length === </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> && numberRegexp.test(s);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> = ZipCodeValidator;</span></code></div></pre>\n<h5 id=\"testts-2\" style=\"position:relative;\"><a href=\"#testts-2\" aria-label=\"testts 2 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>Test.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> zip = </span><span style=\"color: #0000FF\">require</span><span style=\"color: #000000\">(</span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #008000\">// Some samples to try</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> strings = [</span><span style=\"color: #A31515\">\"Hello\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"98052\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"101\"</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #008000\">// Validators to use</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> validator = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> zip();</span>\n\n<span style=\"color: #008000\">// Show whether each string passed each validator</span>\n<span style=\"color: #000000\">strings.forEach(s </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">  console.log(</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">`\"</span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">s</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">\" - </span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">validator.isAcceptable(s) </span><span style=\"color: #000000\">?</span><span style=\"color: #A31515\"> \"matches\" </span><span style=\"color: #000000\">:</span><span style=\"color: #A31515\"> \"does not match\"</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">`</span>\n<span style=\"color: #000000\">  );</span>\n<span style=\"color: #000000\">});</span></code></div></pre>\n<h1 id=\"code-generation-for-modules\" style=\"position:relative;\"><a href=\"#code-generation-for-modules\" aria-label=\"code generation for 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>Code Generation for Modules</h1>\n<p>Depending on the module target specified during compilation, the compiler will generate appropriate code for Node.js (<a href=\"http://wiki.commonjs.org/wiki/CommonJS\">CommonJS</a>), require.js (<a href=\"https://github.com/amdjs/amdjs-api/wiki/AMD\">AMD</a>), <a href=\"https://github.com/umdjs/umd\">UMD</a>, <a href=\"https://github.com/systemjs/systemjs\">SystemJS</a>, or <a href=\"http://www.ecma-international.org/ecma-262/6.0/#sec-modules\">ECMAScript 2015 native modules</a> (ES6) module-loading systems.\nFor more information on what the <code>define</code>, <code>require</code> and <code>register</code> calls in the generated code do, consult the documentation for each module loader.</p>\n<p>This simple example shows how the names used during importing and exporting get translated into the module loading code.</p>\n<h5 id=\"simplemodulets\" style=\"position:relative;\"><a href=\"#simplemodulets\" aria-label=\"simplemodulets 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>SimpleModule.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> m = </span><span style=\"color: #0000FF\">require</span><span style=\"color: #000000\">(</span><span style=\"color: #A31515\">\"mod\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> t = m.something + </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h5 id=\"amd--requirejs-simplemodulejs\" style=\"position:relative;\"><a href=\"#amd--requirejs-simplemodulejs\" aria-label=\"amd  requirejs simplemodulejs 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>AMD / RequireJS SimpleModule.js</h5>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">define([</span><span style=\"color: #A31515\">\"require\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"exports\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"./mod\"</span><span style=\"color: #000000\">], </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(require, exports, mod_1) {</span>\n<span style=\"color: #000000\">  exports.t = mod_1.something + </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">});</span></code></div></pre>\n<h5 id=\"commonjs--node-simplemodulejs\" style=\"position:relative;\"><a href=\"#commonjs--node-simplemodulejs\" aria-label=\"commonjs  node simplemodulejs 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>CommonJS / Node SimpleModule.js</h5>\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\"> mod_1 = require(</span><span style=\"color: #A31515\">\"./mod\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">exports.t = mod_1.something + </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h5 id=\"umd-simplemodulejs\" style=\"position:relative;\"><a href=\"#umd-simplemodulejs\" aria-label=\"umd simplemodulejs 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>UMD SimpleModule.js</h5>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">(</span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(factory) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> module === </span><span style=\"color: #A31515\">\"object\"</span><span style=\"color: #000000\"> && </span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> module.exports === </span><span style=\"color: #A31515\">\"object\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> v = factory(require, exports);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (v !== </span><span style=\"color: #0000FF\">undefined</span><span style=\"color: #000000\">) module.exports = v;</span>\n<span style=\"color: #000000\">  } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> define === </span><span style=\"color: #A31515\">\"function\"</span><span style=\"color: #000000\"> && define.amd) {</span>\n<span style=\"color: #000000\">    define([</span><span style=\"color: #A31515\">\"require\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"exports\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"./mod\"</span><span style=\"color: #000000\">], factory);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">})(</span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(require, exports) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> mod_1 = require(</span><span style=\"color: #A31515\">\"./mod\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  exports.t = mod_1.something + </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">});</span></code></div></pre>\n<h5 id=\"system-simplemodulejs\" style=\"position:relative;\"><a href=\"#system-simplemodulejs\" aria-label=\"system simplemodulejs 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>System SimpleModule.js</h5>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">System.register([</span><span style=\"color: #A31515\">\"./mod\"</span><span style=\"color: #000000\">], </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(exports_1) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> mod_1;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> t;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">    setters: [</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(mod_1_1) {</span>\n<span style=\"color: #000000\">        mod_1 = mod_1_1;</span>\n<span style=\"color: #000000\">      }</span>\n<span style=\"color: #000000\">    ],</span>\n<span style=\"color: #000000\">    execute: </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">      exports_1(</span><span style=\"color: #A31515\">\"t\"</span><span style=\"color: #000000\">, (t = mod_1.something + </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">));</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  };</span>\n<span style=\"color: #000000\">});</span></code></div></pre>\n<h5 id=\"native-ecmascript-2015-modules-simplemodulejs\" style=\"position:relative;\"><a href=\"#native-ecmascript-2015-modules-simplemodulejs\" aria-label=\"native ecmascript 2015 modules simplemodulejs 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>Native ECMAScript 2015 modules SimpleModule.js</h5>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { something } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./mod\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> t = something + </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h1 id=\"simple-example\" style=\"position:relative;\"><a href=\"#simple-example\" aria-label=\"simple 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>Simple Example</h1>\n<p>Below, we’ve consolidated the Validator implementations used in previous examples to only export a single named export from each module.</p>\n<p>To compile, we must specify a module target on the command line. For Node.js, use <code>--module commonjs</code>;\nfor require.js, use <code>--module amd</code>. For example:</p>\n<pre><code class=\"language-Shell\">tsc --module commonjs Test.ts\n</code></pre>\n<p>When compiled, each module will become a separate <code>.js</code> file.\nAs with reference tags, the compiler will follow <code>import</code> statements to compile dependent files.</p>\n<h5 id=\"validationts\" style=\"position:relative;\"><a href=\"#validationts\" aria-label=\"validationts 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>Validation.ts</h5>\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\">interface</span><span style=\"color: #000000\"> StringValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string): boolean;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"lettersonlyvalidatorts\" style=\"position:relative;\"><a href=\"#lettersonlyvalidatorts\" aria-label=\"lettersonlyvalidatorts 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>LettersOnlyValidator.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { StringValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./Validation\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> lettersRegexp =</span><span style=\"color: #A31515\"> /</span><span style=\"color: #0000FF\">^</span><span style=\"color: #A31515\">[A-Za-z]</span><span style=\"color: #000000\">+</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> LettersOnlyValidator </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> StringValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> lettersRegexp.test(s);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"zipcodevalidatorts-3\" style=\"position:relative;\"><a href=\"#zipcodevalidatorts-3\" aria-label=\"zipcodevalidatorts 3 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>ZipCodeValidator.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { StringValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./Validation\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> numberRegexp =</span><span style=\"color: #A31515\"> /</span><span style=\"color: #0000FF\">^</span><span style=\"color: #A31515\">[0-9]</span><span style=\"color: #000000\">+</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> ZipCodeValidator </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> StringValidator {</span>\n<span style=\"color: #000000\">  isAcceptable(s: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> s.length === </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> && numberRegexp.test(s);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"testts-3\" style=\"position:relative;\"><a href=\"#testts-3\" aria-label=\"testts 3 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>Test.ts</h5>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { StringValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./Validation\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { ZipCodeValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { LettersOnlyValidator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./LettersOnlyValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #008000\">// Some samples to try</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> strings = [</span><span style=\"color: #A31515\">\"Hello\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"98052\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"101\"</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #008000\">// Validators to use</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> validators: { [s: string]: StringValidator } = {};</span>\n<span style=\"color: #000000\">validators[</span><span style=\"color: #A31515\">\"ZIP code\"</span><span style=\"color: #000000\">] = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> ZipCodeValidator();</span>\n<span style=\"color: #000000\">validators[</span><span style=\"color: #A31515\">\"Letters only\"</span><span style=\"color: #000000\">] = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> LettersOnlyValidator();</span>\n\n<span style=\"color: #008000\">// Show whether each string passed each validator</span>\n<span style=\"color: #000000\">strings.forEach(s </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> name </span><span style=\"color: #0000FF\">in</span><span style=\"color: #000000\"> validators) {</span>\n<span style=\"color: #000000\">    console.log(</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #A31515\">`\"</span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">s</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">\" - </span><span style=\"color: #0000FF\">${</span>\n<span style=\"color: #A31515\">        validators[name].isAcceptable(s) </span><span style=\"color: #000000\">?</span><span style=\"color: #A31515\"> \"matches\" </span><span style=\"color: #000000\">:</span><span style=\"color: #A31515\"> \"does not match\"</span>\n<span style=\"color: #A31515\">      </span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\"> </span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">name</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">`</span>\n<span style=\"color: #000000\">    );</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">});</span></code></div></pre>\n<h1 id=\"optional-module-loading-and-other-advanced-loading-scenarios\" style=\"position:relative;\"><a href=\"#optional-module-loading-and-other-advanced-loading-scenarios\" aria-label=\"optional module loading and other advanced loading scenarios 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 Module Loading and Other Advanced Loading Scenarios</h1>\n<p>In some cases, you may want to only load a module under some conditions.\nIn TypeScript, we can use the pattern shown below to implement this and other advanced loading scenarios to directly invoke the module loaders without losing type safety.</p>\n<p>The compiler detects whether each module is used in the emitted JavaScript.\nIf a module identifier is only ever used as part of a type annotations and never as an expression, then no <code>require</code> call is emitted for that module.\nThis elision of unused references is a good performance optimization, and also allows for optional loading of those modules.</p>\n<p>The core idea of the pattern is that the <code>import id = require(\"...\")</code> statement gives us access to the types exposed by the module.\nThe module loader is invoked (through <code>require</code>) dynamically, as shown in the <code>if</code> blocks below.\nThis leverages the reference-elision optimization so that the module is only loaded when needed.\nFor this pattern to work, it’s important that the symbol defined via an <code>import</code> is only used in type positions (i.e. never in a position that would be emitted into the JavaScript).</p>\n<p>To maintain type safety, we can use the <code>typeof</code> keyword.\nThe <code>typeof</code> keyword, when used in a type position, produces the type of a value, in this case the type of the module.</p>\n<h5 id=\"dynamic-module-loading-in-nodejs\" style=\"position:relative;\"><a href=\"#dynamic-module-loading-in-nodejs\" aria-label=\"dynamic module loading in nodejs 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>Dynamic Module Loading in Node.js</h5>\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\"> require(moduleName: string): any;</span>\n\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { ZipCodeValidator </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> Zip } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (needZipValidation) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> ZipCodeValidator: </span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> Zip = require(</span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> validator = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> ZipCodeValidator();</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (validator.isAcceptable(</span><span style=\"color: #A31515\">\"...\"</span><span style=\"color: #000000\">)) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">/* ... */</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"sample-dynamic-module-loading-in-requirejs\" style=\"position:relative;\"><a href=\"#sample-dynamic-module-loading-in-requirejs\" aria-label=\"sample dynamic module loading in requirejs 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>Sample: Dynamic Module Loading in require.js</h5>\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\"> require(</span>\n<span style=\"color: #000000\">  moduleNames: string[],</span>\n<span style=\"color: #000000\">  onLoad: (...args: any[]) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> void</span>\n<span style=\"color: #000000\">): void;</span>\n\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\"> Zip </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (needZipValidation) {</span>\n<span style=\"color: #000000\">  require([</span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">], (ZipCodeValidator: </span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> Zip) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> validator = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> ZipCodeValidator.ZipCodeValidator();</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (validator.isAcceptable(</span><span style=\"color: #A31515\">\"...\"</span><span style=\"color: #000000\">)) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #008000\">/* ... */</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  });</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h5 id=\"sample-dynamic-module-loading-in-systemjs\" style=\"position:relative;\"><a href=\"#sample-dynamic-module-loading-in-systemjs\" aria-label=\"sample dynamic module loading in systemjs 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>Sample: Dynamic Module Loading in System.js</h5>\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\">const</span><span style=\"color: #000000\"> System: any;</span>\n\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { ZipCodeValidator </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> Zip } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (needZipValidation) {</span>\n<span style=\"color: #000000\">  System.import(</span><span style=\"color: #A31515\">\"./ZipCodeValidator\"</span><span style=\"color: #000000\">).then((ZipCodeValidator: </span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> Zip) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> x = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> ZipCodeValidator();</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (x.isAcceptable(</span><span style=\"color: #A31515\">\"...\"</span><span style=\"color: #000000\">)) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #008000\">/* ... */</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  });</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h1 id=\"working-with-other-javascript-libraries\" style=\"position:relative;\"><a href=\"#working-with-other-javascript-libraries\" aria-label=\"working with other javascript libraries 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>Working with Other JavaScript Libraries</h1>\n<p>To describe the shape of libraries not written in TypeScript, we need to declare the API that the library exposes.</p>\n<p>We call declarations that don’t define an implementation “ambient”.\nTypically, these are defined in <code>.d.ts</code> files.\nIf you’re familiar with C/C++, you can think of these as <code>.h</code> files.\nLet’s look at a few examples.</p>\n<h2 id=\"ambient-modules\" style=\"position:relative;\"><a href=\"#ambient-modules\" aria-label=\"ambient 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>Ambient Modules</h2>\n<p>In Node.js, most tasks are accomplished by loading one or more modules.\nWe could define each module in its own <code>.d.ts</code> file with top-level export declarations, but it’s more convenient to write them as one larger <code>.d.ts</code> file.\nTo do so, we use a construct similar to ambient namespaces, but we use the <code>module</code> keyword and the quoted name of the module which will be available to a later import.\nFor example:</p>\n<h5 id=\"nodedts-simplified-excerpt\" style=\"position:relative;\"><a href=\"#nodedts-simplified-excerpt\" aria-label=\"nodedts simplified excerpt 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>node.d.ts (simplified excerpt)</h5>\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\">module</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"url\"</span><span style=\"color: #000000\"> {</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\"> Url {</span>\n<span style=\"color: #000000\">    protocol?: string;</span>\n<span style=\"color: #000000\">    hostname?: string;</span>\n<span style=\"color: #000000\">    pathname?: 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\">function</span><span style=\"color: #000000\"> parse(</span>\n<span style=\"color: #000000\">    urlStr: string,</span>\n<span style=\"color: #000000\">    parseQueryString?,</span>\n<span style=\"color: #000000\">    slashesDenoteHost?</span>\n<span style=\"color: #000000\">  ): Url;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">module</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"path\"</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> normalize(p: string): string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> join(...paths: any[]): string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> sep: string;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Now we can <code>/// &#x3C;reference></code> <code>node.d.ts</code> and then load the modules using <code>import url = require(\"url\");</code> or <code>import * as URL from \"url\"</code>.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">/// </span><span style=\"color: #800000\">&lt;reference</span><span style=\"color: #008000\"> </span><span style=\"color: #DC0000\">path</span><span style=\"color: #000000\">=</span><span style=\"color: #A31515\">\"node.d.ts\"</span><span style=\"color: #800000\">/&gt;</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\"> URL </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"url\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myUrl = URL.parse(</span><span style=\"color: #A31515\">\"http://www.typescriptlang.org\"</span><span style=\"color: #000000\">);</span></code></div></pre>\n<h3 id=\"shorthand-ambient-modules\" style=\"position:relative;\"><a href=\"#shorthand-ambient-modules\" aria-label=\"shorthand ambient 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>Shorthand ambient modules</h3>\n<p>If you don’t want to take the time to write out declarations before using a new module, you can use a shorthand declaration to get started quickly.</p>\n<h5 id=\"declarationsdts\" style=\"position:relative;\"><a href=\"#declarationsdts\" aria-label=\"declarationsdts 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>declarations.d.ts</h5>\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\">module</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"hot-new-module\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>All imports from a shorthand module will have the <code>any</code> type.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> x, { y } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"hot-new-module\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">x(y);</span></code></div></pre>\n<h3 id=\"wildcard-module-declarations\" style=\"position:relative;\"><a href=\"#wildcard-module-declarations\" aria-label=\"wildcard module declarations 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>Wildcard module declarations</h3>\n<p>Some module loaders such as <a href=\"https://github.com/systemjs/systemjs/blob/master/docs/overview.md#plugin-syntax\">SystemJS</a>\nand <a href=\"https://github.com/amdjs/amdjs-api/blob/master/LoaderPlugins.md\">AMD</a> allow non-JavaScript content to be imported.\nThese typically use a prefix or suffix to indicate the special loading semantics.\nWildcard module declarations can be used to cover these cases.</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\">module</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"*!text\"</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> content: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">default</span><span style=\"color: #000000\"> content;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #008000\">// Some do it the other way around.</span>\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">module</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"json!*\"</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> value: any;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">default</span><span style=\"color: #000000\"> value;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Now you can import things that match <code>\"*!text\"</code> or <code>\"json!*\"</code>.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> fileContent </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./xyz.txt!text\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> data </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"json!http://example.com/data.json\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">console.log(data, fileContent);</span></code></div></pre>\n<h3 id=\"umd-modules\" style=\"position:relative;\"><a href=\"#umd-modules\" aria-label=\"umd 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>UMD modules</h3>\n<p>Some libraries are designed to be used in many module loaders, or with no module loading (global variables).\nThese are known as <a href=\"https://github.com/umdjs/umd\">UMD</a> modules.\nThese libraries can be accessed through either an import or a global variable.\nFor example:</p>\n<h5 id=\"math-libdts\" style=\"position:relative;\"><a href=\"#math-libdts\" aria-label=\"math libdts 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>math-lib.d.ts</h5>\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\"> isPrime(x: number): boolean;</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\"> mathLib;</span></code></div></pre>\n<p>The library can then be used as an import within modules:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { isPrime } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"math-lib\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">isPrime(</span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">mathLib.isPrime(</span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// ERROR: can't use the global definition from inside a module</span></code></div></pre>\n<p>It can also be used as a global variable, but only inside of a script.\n(A script is a file with no imports or exports.)</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">mathLib.isPrime(</span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">);</span></code></div></pre>\n<h1 id=\"guidance-for-structuring-modules\" style=\"position:relative;\"><a href=\"#guidance-for-structuring-modules\" aria-label=\"guidance for structuring 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>Guidance for structuring modules</h1>\n<h2 id=\"export-as-close-to-top-level-as-possible\" style=\"position:relative;\"><a href=\"#export-as-close-to-top-level-as-possible\" aria-label=\"export as close to top level as possible 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>Export as close to top-level as possible</h2>\n<p>Consumers of your module should have as little friction as possible when using things that you export.\nAdding too many levels of nesting tends to be cumbersome, so think carefully about how you want to structure things.</p>\n<p>Exporting a namespace from your module is an example of adding too many layers of nesting.\nWhile namespaces sometime have their uses, they add an extra level of indirection when using modules.\nThis can quickly become a pain point for users, and is usually unnecessary.</p>\n<p>Static methods on an exported class have a similar problem - the class itself adds a layer of nesting.\nUnless it increases expressivity or intent in a clearly useful way, consider simply exporting a helper function.</p>\n<h3 id=\"if-youre-only-exporting-a-single-class-or-function-use-export-default\" style=\"position:relative;\"><a href=\"#if-youre-only-exporting-a-single-class-or-function-use-export-default\" aria-label=\"if youre only exporting a single class or function use export default 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>If you’re only exporting a single <code>class</code> or <code>function</code>, use <code>export default</code></h3>\n<p>Just as “exporting near the top-level” reduces friction on your module’s consumers, so does introducing a default export.\nIf a module’s primary purpose is to house one specific export, then you should consider exporting it as a default export.\nThis makes both importing and actually using the import a little easier.\nFor example:</p>\n<h4 id=\"myclassts\" style=\"position:relative;\"><a href=\"#myclassts\" aria-label=\"myclassts 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>MyClass.ts</h4>\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\">class</span><span style=\"color: #000000\"> SomeType {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">() { ... }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h4 id=\"myfuncts\" style=\"position:relative;\"><a href=\"#myfuncts\" aria-label=\"myfuncts 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>MyFunc.ts</h4>\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\"> getThing() {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"thing\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h4 id=\"consumerts\" style=\"position:relative;\"><a href=\"#consumerts\" aria-label=\"consumerts 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>Consumer.ts</h4>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> t </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./MyClass\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> f </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./MyFunc\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> x = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> t();</span>\n<span style=\"color: #000000\">console.log(f());</span></code></div></pre>\n<p>This is optimal for consumers. They can name your type whatever they want (<code>t</code> in this case) and don’t have to do any excessive dotting to find your objects.</p>\n<h3 id=\"if-youre-exporting-multiple-objects-put-them-all-at-top-level\" style=\"position:relative;\"><a href=\"#if-youre-exporting-multiple-objects-put-them-all-at-top-level\" aria-label=\"if youre exporting multiple objects put them all at top level 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>If you’re exporting multiple objects, put them all at top-level</h3>\n<h4 id=\"mythingsts\" style=\"position:relative;\"><a href=\"#mythingsts\" aria-label=\"mythingsts 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>MyThings.ts</h4>\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\">class</span><span style=\"color: #000000\"> SomeType {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">/* ... */</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\"> someFunc() {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">/* ... */</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Conversely when importing:</p>\n<h3 id=\"explicitly-list-imported-names\" style=\"position:relative;\"><a href=\"#explicitly-list-imported-names\" aria-label=\"explicitly list imported names 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>Explicitly list imported names</h3>\n<h4 id=\"consumerts-1\" style=\"position:relative;\"><a href=\"#consumerts-1\" aria-label=\"consumerts 1 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>Consumer.ts</h4>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { SomeType, someFunc } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./MyThings\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> x = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> SomeType();</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> y = someFunc();</span></code></div></pre>\n<h3 id=\"use-the-namespace-import-pattern-if-youre-importing-a-large-number-of-things\" style=\"position:relative;\"><a href=\"#use-the-namespace-import-pattern-if-youre-importing-a-large-number-of-things\" aria-label=\"use the namespace import pattern if youre importing a large number of things 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>Use the namespace import pattern if you’re importing a large number of things</h3>\n<h4 id=\"mylargemodulets\" style=\"position:relative;\"><a href=\"#mylargemodulets\" aria-label=\"mylargemodulets 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>MyLargeModule.ts</h4>\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\">class</span><span style=\"color: #000000\"> Dog { ... }</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Cat { ... }</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Tree { ... }</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Flower { ... }</span></code></div></pre>\n<h4 id=\"consumerts-2\" style=\"position:relative;\"><a href=\"#consumerts-2\" aria-label=\"consumerts 2 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>Consumer.ts</h4>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><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\"> myLargeModule </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./MyLargeModule.ts\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> x = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> myLargeModule.Dog();</span></code></div></pre>\n<h2 id=\"re-export-to-extend\" style=\"position:relative;\"><a href=\"#re-export-to-extend\" aria-label=\"re export to extend 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>Re-export to extend</h2>\n<p>Often you will need to extend functionality on a module.\nA common JS pattern is to augment the original object with <em>extensions</em>, similar to how JQuery extensions work.\nAs we’ve mentioned before, modules do not <em>merge</em> like global namespace objects would.\nThe recommended solution is to <em>not</em> mutate the original object, but rather export a new entity that provides the new functionality.</p>\n<p>Consider a simple calculator implementation defined in module <code>Calculator.ts</code>.\nThe module also exports a helper function to test the calculator functionality by passing a list of input strings and writing the result at the end.</p>\n<h4 id=\"calculatorts\" style=\"position:relative;\"><a href=\"#calculatorts\" aria-label=\"calculatorts 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>Calculator.ts</h4>\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\">class</span><span style=\"color: #000000\"> Calculator {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> current = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> memory = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> operator: string;</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">protected</span><span style=\"color: #000000\"> processDigit(digit: string, currentValue: number) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (digit &gt;= </span><span style=\"color: #A31515\">\"0\"</span><span style=\"color: #000000\"> && digit &lt;= </span><span style=\"color: #A31515\">\"9\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> currentValue * </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\"> + (digit.charCodeAt(</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">) - </span><span style=\"color: #A31515\">\"0\"</span><span style=\"color: #000000\">.charCodeAt(</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\">  </span><span style=\"color: #0000FF\">protected</span><span style=\"color: #000000\"> processOperator(operator: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> ([</span><span style=\"color: #A31515\">\"+\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"-\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"*\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"/\"</span><span style=\"color: #000000\">].indexOf(operator) &gt;= </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> operator;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">protected</span><span style=\"color: #000000\"> evaluateOperator(</span>\n<span style=\"color: #000000\">    operator: string,</span>\n<span style=\"color: #000000\">    left: number,</span>\n<span style=\"color: #000000\">    right: number</span>\n<span style=\"color: #000000\">  ): number {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">switch</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.operator) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">case</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"+\"</span><span style=\"color: #000000\">:</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> left + right;</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">case</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"-\"</span><span style=\"color: #000000\">:</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> left - right;</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">case</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"*\"</span><span style=\"color: #000000\">:</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> left * right;</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">case</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"/\"</span><span style=\"color: #000000\">:</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> left / right;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> evaluate() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.operator) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.memory = </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.evaluateOperator(</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.operator,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.memory,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.current</span>\n<span style=\"color: #000000\">      );</span>\n<span style=\"color: #000000\">    } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.memory = </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.current;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.current = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> handleChar(char: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (char === </span><span style=\"color: #A31515\">\"=\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.evaluate();</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> value = </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.processDigit(char, </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.current);</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (value !== </span><span style=\"color: #0000FF\">undefined</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.current = value;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">      } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> value = </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.processOperator(char);</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (value !== </span><span style=\"color: #0000FF\">undefined</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">          </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.evaluate();</span>\n<span style=\"color: #000000\">          </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.operator = value;</span>\n<span style=\"color: #000000\">          </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">        }</span>\n<span style=\"color: #000000\">      }</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">throw</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Error(</span><span style=\"color: #A31515\">`Unsupported input: '</span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">char</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">'`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> getResult() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.memory;</span>\n<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: #0000FF\">function</span><span style=\"color: #000000\"> test(c: Calculator, input: string) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> i = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">; i &lt; input.length; i++) {</span>\n<span style=\"color: #000000\">    c.handleChar(input[i]);</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  console.log(</span><span style=\"color: #A31515\">`result of '</span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">input</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">' is '</span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">c.getResult()</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">'`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Here is a simple test for the calculator using the exposed <code>test</code> function.</p>\n<h4 id=\"testcalculatorts\" style=\"position:relative;\"><a href=\"#testcalculatorts\" aria-label=\"testcalculatorts 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>TestCalculator.ts</h4>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { Calculator, test } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./Calculator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> c = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Calculator();</span>\n<span style=\"color: #000000\">test(c, </span><span style=\"color: #A31515\">\"1+2*33/11=\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// prints 9</span></code></div></pre>\n<p>Now to extend this to add support for input with numbers in bases other than 10, let’s create <code>ProgrammerCalculator.ts</code></p>\n<h4 id=\"programmercalculatorts\" style=\"position:relative;\"><a href=\"#programmercalculatorts\" aria-label=\"programmercalculatorts 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>ProgrammerCalculator.ts</h4>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { Calculator } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./Calculator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> ProgrammerCalculator </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Calculator {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">static</span><span style=\"color: #000000\"> digits = [</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"0\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"1\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"2\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"3\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"4\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"5\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"6\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"7\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"8\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"9\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"A\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"B\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"C\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"D\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"E\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"F\"</span>\n<span style=\"color: #000000\">  ];</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> base: number) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">();</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> maxBase = ProgrammerCalculator.digits.length;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (base &lt;= </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\"> || base &gt; maxBase) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">throw</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Error(</span><span style=\"color: #A31515\">`base has to be within 0 to </span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">maxBase</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\"> inclusive.`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">protected</span><span style=\"color: #000000\"> processDigit(digit: string, currentValue: number) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (ProgrammerCalculator.digits.indexOf(digit) &gt;= </span><span style=\"color: #09835A\">0</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\">        currentValue * </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.base + ProgrammerCalculator.digits.indexOf(digit)</span>\n<span style=\"color: #000000\">      );</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// Export the new extended calculator as Calculator</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> { ProgrammerCalculator </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> Calculator };</span>\n\n<span style=\"color: #008000\">// Also, export the helper function</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> { test } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./Calculator\"</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>The new module <code>ProgrammerCalculator</code> exports an API shape similar to that of the original <code>Calculator</code> module, but does not augment any objects in the original module.\nHere is a test for our ProgrammerCalculator class:</p>\n<h4 id=\"testprogrammercalculatorts\" style=\"position:relative;\"><a href=\"#testprogrammercalculatorts\" aria-label=\"testprogrammercalculatorts 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>TestProgrammerCalculator.ts</h4>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { Calculator, test } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./ProgrammerCalculator\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> c = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Calculator(</span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">test(c, </span><span style=\"color: #A31515\">\"001+010=\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// prints 3</span></code></div></pre>\n<h2 id=\"do-not-use-namespaces-in-modules\" style=\"position:relative;\"><a href=\"#do-not-use-namespaces-in-modules\" aria-label=\"do not use namespaces 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>Do not use namespaces in modules</h2>\n<p>When first moving to a module-based organization, a common tendency is to wrap exports in an additional layer of namespaces.\nModules have their own scope, and only exported declarations are visible from outside the module.\nWith this in mind, namespace provide very little, if any, value when working with modules.</p>\n<p>On the organization front, namespaces are handy for grouping together logically-related objects and types in the global scope.\nFor example, in C#, you’re going to find all the collection types in System.Collections.\nBy organizing our types into hierarchical namespaces, we provide a good “discovery” experience for users of those types.\nModules, on the other hand, are already present in a file system, necessarily.\nWe have to resolve them by path and filename, so there’s a logical organization scheme for us to use.\nWe can have a /collections/generic/ folder with a list module in it.</p>\n<p>Namespaces are important to avoid naming collisions in the global scope.\nFor example, you might have <code>My.Application.Customer.AddForm</code> and <code>My.Application.Order.AddForm</code> — two types with the same name, but a different namespace.\nThis, however, is not an issue with modules.\nWithin a module, there’s no plausible reason to have two objects with the same name.\nFrom the consumption side, the consumer of any given module gets to pick the name that they will use to refer to the module, so accidental naming conflicts are impossible.</p>\n<blockquote>\n<p>For more discussion about modules and namespaces see <a href=\"./Namespaces%20and%20Modules.md\">Namespaces and Modules</a>.</p>\n</blockquote>\n<h2 id=\"red-flags\" style=\"position:relative;\"><a href=\"#red-flags\" aria-label=\"red flags 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>Red Flags</h2>\n<p>All of the following are red flags for module structuring. Double-check that you’re not trying to namespace your external modules if any of these apply to your files:</p>\n<ul>\n<li>A file whose only top-level declaration is <code>export namespace Foo { ... }</code> (remove <code>Foo</code> and move everything ‘up’ a level)</li>\n<li>Multiple files that have the same <code>export namespace Foo {</code> at top-level (don’t think that these are going to combine into one <code>Foo</code>!)</li>\n</ul>","headings":[{"value":"Introduction","depth":1},{"value":"Export","depth":1},{"value":"Exporting a declaration","depth":2},{"value":"StringValidator.ts","depth":5},{"value":"ZipCodeValidator.ts","depth":5},{"value":"Export statements","depth":2},{"value":"Re-exports","depth":2},{"value":"ParseIntBasedZipCodeValidator.ts","depth":5},{"value":"AllValidators.ts","depth":5},{"value":"Import","depth":1},{"value":"Import a single export from a module","depth":2},{"value":"Import the entire module into a single variable, and use it to access the module exports","depth":2},{"value":"Import a module for side-effects only","depth":2},{"value":"Importing Types","depth":2},{"value":"Default exports","depth":1},{"value":"JQuery.d.ts","depth":5},{"value":"App.ts","depth":5},{"value":"ZipCodeValidator.ts","depth":5},{"value":"Test.ts","depth":5},{"value":"StaticZipCodeValidator.ts","depth":5},{"value":"Test.ts","depth":5},{"value":"OneTwoThree.ts","depth":5},{"value":"Log.ts","depth":5},{"value":"Export all as x","depth":2},{"value":"export = and import = require()","depth":1},{"value":"ZipCodeValidator.ts","depth":5},{"value":"Test.ts","depth":5},{"value":"Code Generation for Modules","depth":1},{"value":"SimpleModule.ts","depth":5},{"value":"AMD / RequireJS SimpleModule.js","depth":5},{"value":"CommonJS / Node SimpleModule.js","depth":5},{"value":"UMD SimpleModule.js","depth":5},{"value":"System SimpleModule.js","depth":5},{"value":"Native ECMAScript 2015 modules SimpleModule.js","depth":5},{"value":"Simple Example","depth":1},{"value":"Validation.ts","depth":5},{"value":"LettersOnlyValidator.ts","depth":5},{"value":"ZipCodeValidator.ts","depth":5},{"value":"Test.ts","depth":5},{"value":"Optional Module Loading and Other Advanced Loading Scenarios","depth":1},{"value":"Dynamic Module Loading in Node.js","depth":5},{"value":"Sample: Dynamic Module Loading in require.js","depth":5},{"value":"Sample: Dynamic Module Loading in System.js","depth":5},{"value":"Working with Other JavaScript Libraries","depth":1},{"value":"Ambient Modules","depth":2},{"value":"node.d.ts (simplified excerpt)","depth":5},{"value":"Shorthand ambient modules","depth":3},{"value":"declarations.d.ts","depth":5},{"value":"Wildcard module declarations","depth":3},{"value":"UMD modules","depth":3},{"value":"math-lib.d.ts","depth":5},{"value":"Guidance for structuring modules","depth":1},{"value":"Export as close to top-level as possible","depth":2},{"value":"If you’re only exporting a single class or function, use export default","depth":3},{"value":"MyClass.ts","depth":4},{"value":"MyFunc.ts","depth":4},{"value":"Consumer.ts","depth":4},{"value":"If you’re exporting multiple objects, put them all at top-level","depth":3},{"value":"MyThings.ts","depth":4},{"value":"Explicitly list imported names","depth":3},{"value":"Consumer.ts","depth":4},{"value":"Use the namespace import pattern if you’re importing a large number of things","depth":3},{"value":"MyLargeModule.ts","depth":4},{"value":"Consumer.ts","depth":4},{"value":"Re-export to extend","depth":2},{"value":"Calculator.ts","depth":4},{"value":"TestCalculator.ts","depth":4},{"value":"ProgrammerCalculator.ts","depth":4},{"value":"TestProgrammerCalculator.ts","depth":4},{"value":"Do not use namespaces in modules","depth":2},{"value":"Red Flags","depth":2}],"frontmatter":{"permalink":"/docs/handbook/modules.html","title":"Modules"}}},"pageContext":{"slug":"/docs/handbook/modules.html","isOldHandbook":true}}}