Modules are a way of organizing and structuring the code into reusable and maintainable code blocks which can be imported into any other file where we need the same kind of code. It enhances the reusability, maintainability, and readability of our code which helps to improve the web performance of the website. You can import and export the TypeScript modules in the following ways:
- Using the require() method:
// Exporting module
export default function_name(): void {
// Function statements
}
// Importing module using require() method
const importedVariableName =
require('Path of the file')
- Using the import statement:
// Exporting module
export default function_name(): void {
// Function statements
}
// Importing module using rimport statement
import nameWithWhichYouWantToImportFile from 'filePath';