- TypeScript Tutorial
- TypeScript Home
- TypeScript Introduction
- TypeScript Setup
- TypeScript First Program
- TypeScript vs JavaScript
- TypeScript Data Types
- TypeScript Type Inference
- TypeScript Type Annotations
- TypeScript Interfaces
- TypeScript Enums
- TypeScript Type Aliases
- TypeScript Type Assertions
- TypeScript Variables
- TypeScript Functions
- TypeScript Functions
- TypeScript Optional Parameters
- TypeScript Default Parameters
- TypeScript Rest Parameters
- TypeScript Arrow Functions
- Classes and Objects
- Introduction to Classes
- Properties and Methods
- Access Modifiers
- Static Members
- Inheritance
- Abstract Classes
- Interfaces vs Classes
- Advanced Types
- TypeScript Union Types
- TypeScript Intersection Types
- TypeScript Literal Types
- TypeScript Nullable Types
- TypeScript Type Guards
- TypeScript Discriminated Unions
- TypeScript Index Signatures
- TypeScript Generics
- Introduction to Generics
- TypeScript Generic Functions
- TypeScript Generic Classes
- TypeScript Generic Constraints
- TypeScript Modules
- Introduction to Modules
- TypeScript Import and Export
- TypeScript Default Exports
- TypeScript Namespace
- Decorators
- Introduction to Decorators
- TypeScript Class Decorators
- TypeScript Method Decorators
- TypeScript Property Decorators
- TypeScript Parameter Decorators
- Configuration
- TypeScript tsconfig.json File
- TypeScript Compiler Options
- TypeScript Strict Mode
- TypeScript Watch Mode
TypeScript Introduction to Modules
In TypeScript, modules allow you to structure your code into smaller, reusable, and maintainable components. A module is any file that contains a namespace
, class, function, or variable and exports these components to make them available for use in other files. Modules help in keeping the global scope clean and prevent naming conflicts by encapsulating code within a module boundary.
What are Modules?
Modules are a way to organize TypeScript code into separate, self-contained units that can be imported and exported between different files. A module can be an entire file that contains code you want to share, and it allows you to isolate functionality, which leads to more maintainable and testable code.
TypeScript modules use the export
and import
keywords to export and import components between different files.
How Modules Work in TypeScript
Exporting: You use the export
keyword to expose functions, variables, or classes from a module so that they can be used in other modules.
Importing: The import
keyword allows you to access the exported components from other modules.
Syntax of Modules
1. Exporting from a Module
You can export variables, functions, classes, or interfaces by adding the export
keyword.
Example: Exporting Variables and Functions
- In the above code,
pi
,add()
, andsubtract()
are all exported from themathOperations.ts
module.
2. Importing from a Module
Once components are exported from a module, they can be imported into another file using the import
statement.
Example: Importing Variables and Functions
- In the above example, we import the
pi
,add()
, andsubtract()
from themathOperations.ts
file. - The relative path
./mathOperations
points to themathOperations.ts
file.
Default Exports
You can also export a single component as the default export from a module. This is particularly useful when you have only one main component to export.
Example: Default Export
- Here,
multiply
is the default export fromcalculator.ts
.
Importing a Default Export
- Since
multiply
is the default export, we don't need to use curly braces when importing it.
Renaming Imports
You can also rename imports while importing them into a module, which can be helpful if there are naming conflicts.
Example: Renaming Imports
- In this case,
add()
is imported and renamed tosum
to avoid any name conflicts or to make it more meaningful in the context.
Re-exporting from a Module
You can re-export components from other modules without directly importing them.
Example: Re-exporting
- The
allOperations.ts
file re-exports everything frommathOperations.ts
. Any import toallOperations.ts
will have access to everything inmathOperations.ts
.
Importing from a Re-exported Module
- By importing from
allOperations.ts
, you gain access to all the exports ofmathOperations.ts
.
Namespaces vs Modules
In TypeScript, namespaces and modules are two different ways to organize code. The main difference is that:
- Modules are files that use the
import
andexport
keywords to organize code and avoid polluting the global namespace. - Namespaces are used to organize code within a single file, but they don't support
import
andexport
keywords (and are more commonly used in older TypeScript codebases).
TypeScript Module Resolution
TypeScript uses a module resolution strategy to locate and load modules. There are two types of resolution:
- Classic Resolution (default in TypeScript)
- Node Resolution (used in Node.js environments)
You can configure the module resolution strategy using the moduleResolution
option in tsconfig.json
to make sure TypeScript can correctly find and resolve your modules.
Summary
Modules in TypeScript help structure code into self-contained units that can be easily reused and maintained. Key points include:
- Exporting components using
export
. - Importing components from other modules using
import
. - Default Exports for the main exported component in a module.
- Renaming imports to avoid conflicts.
- Re-exporting modules to combine multiple exports into one file.
- Modules help avoid polluting the global namespace, making code more modular and organized.
Using modules in TypeScript encourages better code organization and promotes the reuse of components, leading to a cleaner and more maintainable codebase.