Introduction
See the documentation for more information about ES Modules.See the Namespaces documentation for more information about TypeScript namespaces.
Note: In very old versions of TypeScript namespaces were called ‘Internal Modules’, these pre-date JavaScript module systems.
Using Modules
Modules can contain both code and declarations.
Modules also have a dependency on a module loader (such as CommonJs/Require.js) or a runtime which supports ES Modules.Modules provide for better code reuse, stronger isolation and better tooling support for bundling.
It is also worth noting that, for Node.js applications, modules are the default and the recommended approach to structure your code.
Using Namespaces
Namespaces are a TypeScript-specific way to organize code.Namespaces are simply named JavaScript objects in the global namespace.This makes namespaces a very simple construct to use.Unlike modules, they can span multiple files, and can be concatenated using .Namespaces can be a good way to structure your code in a Web Application, with all dependencies included as <script>
tags in your HTML page.
Just like all global namespace pollution, it can be hard to identify component dependencies, especially in a large application.
Pitfalls of Namespaces and Modules
In this section we’ll describe various common pitfalls in using namespaces and modules, and how to avoid them.
A common mistake is to try to use the /// <reference … />
syntax to refer to a module file, rather than using an import
statement.To understand the distinction, we first need to understand how the compiler can locate the type information for a module based on the path of an import
(e.g. the …
in import x from "…";
, import x = require("…");
, etc.) path.
The compiler will try to find a .ts
, .tsx
, and then a .d.ts
with the appropriate path.If a specific file could not be found, then the compiler will look for an ambient module declaration.Recall that these need to be declared in a file.
myModules.d.ts
myOtherModule.ts
/// <reference path="myModules.d.ts" />
import * as m from "SomeModule";
If you’re converting a program from namespaces to modules, it can be easy to end up with a file that looks like this:
shapes.ts
The top-level module here Shapes
wraps up Triangle
and Square
for no reason.This is confusing and annoying for consumers of your module:
let t = new shapes.Shapes.Triangle(); // shapes.Shapes?
A key feature of modules in TypeScript is that two different modules will never contribute names to the same scope.Because the consumer of a module decides what name to assign it, there’s no need to proactively wrap up the exported symbols in a namespace.
To reiterate why you shouldn’t try to namespace your module contents, the general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions.Because the module file itself is already a logical grouping, and its top-level name is defined by the code that imports it, it’s unnecessary to use an additional module layer for exported objects.
Here’s a revised example:
shapes.ts
shapeConsumer.ts
import * as shapes from "./shapes";