Difference between Namespaces and ModulesNamespaceA namespace is a way that is used for logical grouping of functionalities. It allows us to organize our code in a much cleaner way. A namespace can include interfaces, classes, functions, and variables to support a group of related functionalities. Unlike JavaScript, namespaces are inbuilt into TypeScript. In JavaScript, the variables declarations go into the global scope. If the multiple JavaScript files are used in the same project, then there will be a possibility of confusing new users by overwriting them with a similar name. Hence, the use of TypeScript namespace removes the naming collisions. A namespace can span in multiple files and allow to concatenate each file using "--outFile" as they were all defined in one place. Namespace Declaration FileName: StoreCalc.ts Accessing Namespace To read more information, click here. ModuleA module is a way to create a group of related variables, functions, classes, and interfaces, etc. It executes in the local scope, not in the global scope. In other words, the variables, functions, classes, and interfaces declared in a module cannot be accessible outside the module directly. We can create a module by using the export keyword and can use in other modules by using the import keyword. Modules import another module by using a module loader. At runtime, the module loader is responsible for locating and executing all dependencies of a module before executing it. The most common modules loaders which are used in JavaScript are the CommonJS module loader for Node.js and require.js for Web applications. Module Declaration FileName: addition.ts Accessing Modules To read more information, click here. Module vs. Namespace
|