Modules | Typescript
Export#
Exporting a declaration#
Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword.
StringValidator.ts#
ZipCodeValidator.ts#
Export statements#
Re-exports#
ParseIntBasedZipCodeValidator.ts#
export * from "module" syntax.
AllValidators.ts#
Import#
Import a module for side-effects only#
Though not recommended practice, some modules set up some global state that can be used by other modules. These modules may not have any exports, or the consumer is not interested in any of their exports. To import these modules, use:
Importing Types#
Prior to TypeScript 3.8, you can import a type using import.
With TypeScript 3.8, you can import a type using the import statement, or using import type.
import type is always guaranteed to be removed from your JavaScript, and tools like Babel can make better assumptions about your code via the isolatedModules compiler flag.
You can read more in the 3.8 release notes.
Default exports#
Classes and function declarations can be authored directly as default exports.
Default export class and function declaration names are optional.
default exports can also be just values:
OneTwoThree.ts#
Log.ts#
export = and import = require()#
The export = syntax specifies a single object that is exported from the module.
This can be a class, interface, namespace, function, or enum.
When exporting a module using export =, TypeScript-specific import module = require("module") must be used to import the module.
ZipCodeValidator.ts#
Test.ts#
Optional Module Loading and Other Advanced Loading Scenarios#
Dynamic Module Loading in Node.js#
Working with Other JavaScript Libraries#
To describe the shape of libraries not written in TypeScript, we need to declare the API that the library exposes.
We call declarations that don't define an implementation "ambient".
Typically, these are defined in .d.ts files.
Ambient Modules#
In Node.js, most tasks are accomplished by loading one or more modules.
We could define each module in its own .d.ts file with top-level export declarations, but it's more convenient to write them as one larger .d.ts file.
To do so, we use a construct similar to ambient namespaces, but we use the module keyword and the quoted name of the module which will be available to a later import.
For example:
node.d.ts (simplified excerpt)#
Now we can /// <reference> node.d.ts and then load the modules using import url = require("url"); or import * as URL from "url".
Shorthand ambient modules#
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.
declarations.d.ts#
All imports from a shorthand module will have the any type.
Wildcard module declarations#
Some module loaders such as SystemJS and AMD allow non-JavaScript content to be imported. These typically use a prefix or suffix to indicate the special loading semantics. Wildcard module declarations can be used to cover these cases.
Now you can import things that match "*!text" or "json!*".
UMD modules#
Some libraries are designed to be used in many module loaders, or with no module loading (global variables). These are known as UMD modules. These libraries can be accessed through either an import or a global variable. For example:
math-lib.d.ts#
The library can then be used as an import within modules:
It can also be used as a global variable, but only inside of a script. (A script is a file with no imports or exports.)
Guidance for structuring modules#
Export as close to top-level as possible#
If you're only exporting a single class or function, use export default#
If you're exporting multiple objects, put them all at top-level#
MyThings.ts#
Conversely when importing:
Explicitly list imported names#
Consumer.ts#
Use the namespace import pattern if you're importing a large number of things#
MyLargeModule.ts#
Consumer.ts#
Re-export to extend#
ProgrammerCalculator.ts#
Do not use namespaces in modules#
When first moving to a module-based organization, a common tendency is to wrap exports in an additional layer of namespaces. Modules have their own scope, and only exported declarations are visible from outside the module. With this in mind, namespace provide very little, if any, value when working with modules.
For more discussion about modules and namespaces see Namespaces and Modules.