Chuck's Academy

Basic TypeScript

Modules and Namespaces in TypeScript

As applications grow in complexity, it's important to organize code into more manageable parts. TypeScript provides two main ways to achieve this: modules and namespaces. In this chapter, we will see how to use them to improve the organization of our projects.

Modules in TypeScript

A module in TypeScript is any file that contains code. Each file in TypeScript can act as an independent module. You can export any part of the code from a file and then import it into other files as needed.

Module Exporting

For a function, class, or variable to be available outside the file in which it is defined, you must export it. There are two main ways to export in TypeScript: named export and default export.

esta imagen muestra un cheatsheat de modulos en typescriptesta imagen muestra un cheatsheat de modulos en typescript

Named Export

With named export, you can export multiple items from a file and specify what you want to import into the other file.

typescript
"This math.ts file exports two functions: add and subtract. These functions can now be imported in other files."

Named Import

Once the items are exported, you can import them into other files using the import syntax.

typescript
"Here we import the functions add and subtract from math.ts, and then use them in the app.ts file."

Default Export

The default export allows you to export a single item from a file. This item can be imported without using its exact name.

typescript
"In this example, the multiply function is exported by default. By doing this, it is not necessary to use the exact name of the function when importing it."

Default Import

When importing a default item, you can use any name to reference it in your file.

typescript
"Here we import the function multiply from math.ts, but we do not need to use its exact name. We can use any name for the import."

Namespaces in TypeScript

Namespaces are another way to organize code, especially when working on large projects. Unlike modules, namespaces group code within a single file or several combined files. Namespaces are useful when you want to keep all related code under the same global name.

Defining a Namespace

To define a namespace, use the namespace keyword followed by a name. Inside this namespace, you can declare classes, functions, variables, etc.

typescript
"In this example, we define a namespace called Geometry that contains a function calculateArea. This function can be accessed using the namespace name followed by the function."

Nested Namespaces

You can nest namespaces within other namespaces to better organize code.

typescript
"This example shows nested namespaces. Inside MathOperations, we have two namespaces: Basic and Advanced, each with their own functions. We access them using the full namespace name."

Conclusion

In this chapter, we have learned how to use modules and namespaces to organize code in TypeScript. Modules are a modern and flexible way to divide code into different files, while namespaces provide a way to group code within a single file or multiple ones. Both are powerful tools that help keep code clean and structured.


Ask me anything