{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/namespaces.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":"8d2fafaf-bad5-5483-b56c-d46c317a2448","excerpt":"A note about terminology:\nIt’s important to note that in TypeScript 1.5, the nomenclature has changed.\n“Internal modules” are now “namespaces”.\n“External…","html":"<blockquote>\n<p><strong>A note about terminology:</strong>\nIt’s important to note that in TypeScript 1.5, the nomenclature has changed.\n“Internal modules” are now “namespaces”.\n“External modules” are now simply “modules”, as to align with <a href=\"http://www.ecma-international.org/ecma-262/6.0/\">ECMAScript 2015</a>’s terminology, (namely that <code>module X {</code> is equivalent to the now-preferred <code>namespace X {</code>).</p>\n</blockquote>\n<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>This post outlines the various ways to organize your code using namespaces (previously “internal modules”) in TypeScript.\nAs we alluded in our note about terminology, “internal modules” are now referred to as “namespaces”.\nAdditionally, anywhere the <code>module</code> keyword was used when declaring an internal module, the <code>namespace</code> keyword can and should be used instead.\nThis avoids confusing new users by overloading them with similarly named terms.</p>\n<h1 id=\"first-steps\" style=\"position:relative;\"><a href=\"#first-steps\" aria-label=\"first steps 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>First steps</h1>\n<p>Let’s start with the program we’ll be using as our example throughout this page.\nWe’ve written a small set of simplistic string validators, as you might write to check a user’s input on a form in a webpage or check the format of an externally-provided data file.</p>\n<h2 id=\"validators-in-a-single-file\" style=\"position:relative;\"><a href=\"#validators-in-a-single-file\" aria-label=\"validators in a single file 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>Validators in a single file</h2>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><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>\n\n<span style=\"color: #0000FF\">let</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<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\n<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>\n\n<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\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: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> s </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> strings) {</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\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> isMatch = validators[name].isAcceptable(s);</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\">isMatch </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: #0000FF\">${</span><span style=\"color: #A31515\">name</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">'.`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h1 id=\"namespacing\" style=\"position:relative;\"><a href=\"#namespacing\" aria-label=\"namespacing 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>Namespacing</h1>\n<p>As we add more validators, we’re going to want to have some kind of organization scheme so that we can keep track of our types and not worry about name collisions with other objects.\nInstead of putting lots of different names into the global namespace, let’s wrap up our objects into a namespace.</p>\n<p>In this example, we’ll move all validator-related entities into a namespace called <code>Validation</code>.\nBecause we want the interfaces and classes here to be visible outside the namespace, we preface them with <code>export</code>.\nConversely, the variables <code>lettersRegexp</code> and <code>numberRegexp</code> are implementation details, so they are left unexported and will not be visible to code outside the namespace.\nIn the test code at the bottom of the file, we now need to qualify the names of the types when used outside the namespace, e.g. <code>Validation.LettersOnlyValidator</code>.</p>\n<h2 id=\"namespaced-validators\" style=\"position:relative;\"><a href=\"#namespaced-validators\" aria-label=\"namespaced validators 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>Namespaced Validators</h2>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Validation {</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\"> StringValidator {</span>\n<span style=\"color: #000000\">    isAcceptable(s: string): boolean;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><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<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: #000000\">  </span><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>\n\n<span style=\"color: #000000\">  </span><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>\n<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]: Validation.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\"> Validation.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\"> Validation.LettersOnlyValidator();</span>\n\n<span style=\"color: #008000\">// Show whether each string passed each validator</span>\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> s </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> strings) {</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=\"splitting-across-files\" style=\"position:relative;\"><a href=\"#splitting-across-files\" aria-label=\"splitting across files 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>Splitting Across Files</h1>\n<p>As our application grows, we’ll want to split the code across multiple files to make it easier to maintain.</p>\n<h2 id=\"multi-file-namespaces\" style=\"position:relative;\"><a href=\"#multi-file-namespaces\" aria-label=\"multi file namespaces 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>Multi-file namespaces</h2>\n<p>Here, we’ll split our <code>Validation</code> namespace across many files.\nEven though the files are separate, they can each contribute to the same namespace and can be consumed as if they were all defined in one place.\nBecause there are dependencies between files, we’ll add reference tags to tell the compiler about the relationships between the files.\nOur test code is otherwise unchanged.</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\">namespace</span><span style=\"color: #000000\"> Validation {</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\"> StringValidator {</span>\n<span style=\"color: #000000\">    isAcceptable(s: string): boolean;</span>\n<span style=\"color: #000000\">  }</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: #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\">\"Validation.ts\"</span><span style=\"color: #008000\"> </span><span style=\"color: #800000\">/&gt;</span>\n<span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Validation {</span>\n<span style=\"color: #000000\">  </span><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<span style=\"color: #000000\">  </span><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>\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: #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\">\"Validation.ts\"</span><span style=\"color: #008000\"> </span><span style=\"color: #800000\">/&gt;</span>\n<span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Validation {</span>\n<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<span style=\"color: #000000\">  </span><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>\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: #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\">\"Validation.ts\"</span><span style=\"color: #008000\"> </span><span style=\"color: #800000\">/&gt;</span>\n<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\">\"LettersOnlyValidator.ts\"</span><span style=\"color: #008000\"> </span><span style=\"color: #800000\">/&gt;</span>\n<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\">\"ZipCodeValidator.ts\"</span><span style=\"color: #008000\"> </span><span style=\"color: #800000\">/&gt;</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]: Validation.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\"> Validation.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\"> Validation.LettersOnlyValidator();</span>\n\n<span style=\"color: #008000\">// Show whether each string passed each validator</span>\n<span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> s </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> strings) {</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<p>Once there are multiple files involved, we’ll need to make sure all of the compiled code gets loaded.\nThere are two ways of doing this.</p>\n<p>First, we can use concatenated output using the <code>--outFile</code> flag to compile all of the input files into a single JavaScript output file:</p>\n<pre><code class=\"language-Shell\">tsc --outFile sample.js Test.ts\n</code></pre>\n<p>The compiler will automatically order the output file based on the reference tags present in the files. You can also specify each file individually:</p>\n<pre><code class=\"language-Shell\">tsc --outFile sample.js Validation.ts LettersOnlyValidator.ts ZipCodeValidator.ts Test.ts\n</code></pre>\n<p>Alternatively, we can use per-file compilation (the default) to emit one JavaScript file for each input file.\nIf multiple JS files get produced, we’ll need to use <code>&#x3C;script></code> tags on our webpage to load each emitted file in the appropriate order, for example:</p>\n<h5 id=\"mytestpagehtml-excerpt\" style=\"position:relative;\"><a href=\"#mytestpagehtml-excerpt\" aria-label=\"mytestpagehtml 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>MyTestPage.html (excerpt)</h5>\n<pre class=\"shiki\"><div class=\"language-id\">html</div><div class='code-container'><code><span style=\"color: #800000\">&lt;script</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">src</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"Validation.js\"</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">type</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"text/javascript\"</span><span style=\"color: #800000\"> /&gt;</span>\n<span style=\"color: #800000\">&lt;script</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">src</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"LettersOnlyValidator.js\"</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">type</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"text/javascript\"</span><span style=\"color: #800000\"> /&gt;</span>\n<span style=\"color: #800000\">&lt;script</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">src</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"ZipCodeValidator.js\"</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">type</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"text/javascript\"</span><span style=\"color: #800000\"> /&gt;</span>\n<span style=\"color: #800000\">&lt;script</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">src</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"Test.js\"</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">type</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"text/javascript\"</span><span style=\"color: #800000\"> /&gt;</span></code></div></pre>\n<h1 id=\"aliases\" style=\"position:relative;\"><a href=\"#aliases\" aria-label=\"aliases 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>Aliases</h1>\n<p>Another way that you can simplify working with namespaces is to use <code>import q = x.y.z</code> to create shorter names for commonly-used objects.\nNot to be confused with the <code>import x = require(\"name\")</code> syntax used to load modules, this syntax simply creates an alias for the specified symbol.\nYou can use these sorts of imports (commonly referred to as aliases) for any kind of identifier, including objects created from module imports.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Shapes {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Polygons {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Triangle {}</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Square {}</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> polygons = Shapes.Polygons;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> sq = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> polygons.Square(); </span><span style=\"color: #008000\">// Same as 'new Shapes.Polygons.Square()'</span></code></div></pre>\n<p>Notice that we don’t use the <code>require</code> keyword; instead we assign directly from the qualified name of the symbol we’re importing.\nThis is similar to using <code>var</code>, but also works on the type and namespace meanings of the imported symbol.\nImportantly, for values, <code>import</code> is a distinct reference from the original symbol, so changes to an aliased <code>var</code> will not be reflected in the original variable.</p>\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.\nBecause most JavaScript libraries expose only a few top-level objects, namespaces are a good way to represent them.</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-namespaces\" style=\"position:relative;\"><a href=\"#ambient-namespaces\" aria-label=\"ambient namespaces 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 Namespaces</h2>\n<p>The popular library D3 defines its functionality in a global object called <code>d3</code>.\nBecause this library is loaded through a <code>&#x3C;script></code> tag (instead of a module loader), its declaration uses namespaces to define its shape.\nFor the TypeScript compiler to see this shape, we use an ambient namespace declaration.\nFor example, we could begin writing it as follows:</p>\n<h5 id=\"d3dts-simplified-excerpt\" style=\"position:relative;\"><a href=\"#d3dts-simplified-excerpt\" aria-label=\"d3dts 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>D3.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\">namespace</span><span style=\"color: #000000\"> D3 {</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\"> Selectors {</span>\n<span style=\"color: #000000\">    select: {</span>\n<span style=\"color: #000000\">      (selector: string): Selection;</span>\n<span style=\"color: #000000\">      (element: EventTarget): Selection;</span>\n<span style=\"color: #000000\">    };</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Event {</span>\n<span style=\"color: #000000\">    x: number;</span>\n<span style=\"color: #000000\">    y: number;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Base </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Selectors {</span>\n<span style=\"color: #000000\">    event: Event;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> d3: D3.Base;</span></code></div></pre>","headings":[{"value":"Introduction","depth":1},{"value":"First steps","depth":1},{"value":"Validators in a single file","depth":2},{"value":"Namespacing","depth":1},{"value":"Namespaced Validators","depth":2},{"value":"Splitting Across Files","depth":1},{"value":"Multi-file namespaces","depth":2},{"value":"Validation.ts","depth":5},{"value":"LettersOnlyValidator.ts","depth":5},{"value":"ZipCodeValidator.ts","depth":5},{"value":"Test.ts","depth":5},{"value":"MyTestPage.html (excerpt)","depth":5},{"value":"Aliases","depth":1},{"value":"Working with Other JavaScript Libraries","depth":1},{"value":"Ambient Namespaces","depth":2},{"value":"D3.d.ts (simplified excerpt)","depth":5}],"frontmatter":{"permalink":"/docs/handbook/namespaces.html","title":"Namespaces"}}},"pageContext":{"slug":"/docs/handbook/namespaces.html","isOldHandbook":true}}}