- A module is designed to organize the code written in TypeScript.
- There are two types of modules
- Internal Modules
- External Modules
Internal Modules
- They are used to group classes, interfaces and functions in one group and can be exported to another module.
- There functionality is pretty much similar to namespaces.
- Internal Modules are still used in some code, but we prefer using namespaces now.
- Syntax
module ModuleName{
export class className{
// statements
}
}
External Modules
- The are used to specify and load dependencies in multiple .js files.
- External modules are not used when there is only one .js file.
- Traditionally we use <script> tags to load .js file in a particular order, but these tags are not available every time, like in NodeJS.
- There are two ways of loading dependent .js files from a single .js file
- Client Side – RequireJS
- Server Side – NodeJS
- The syntax for declaring an external module is using keyword export and import.
Module Loader
- To load JavaScript files externally, we need a module loader.
- Module Loader is another .js library.
- Most common module loader we use is RequireJS. This is an implementation of AMD (Asynchronous Module Definition) specification.
- Instead of loading files one after the other, AMD can load them all separately, even when they are dependent on each other.