← back

Namespaces in Typescript

Cover Image for Namespaces in Typescript

Namespaces in Typescript

In this article we will try to grasp the concept of namespaces and how they are helpful and finally we will learn how to use them in Typescript.

directly jump to coding->

PS: Typescript is my go to language all time here is why.

First of all what are namespaces ?

As per wikipedia definition

In computing, a namespace is a set of signs (names) that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.

Namespaces are used to group related

  • functions
  • interfaces
  • classes

Namespaces are used to avoid naming conflicts, organize code into logical groups and to provide privacy for variables and functions.

Concept of namespace is not new, we have been using them in some databases such as cassandra,

Linux Kernel also uses namespaces to isolate the processes from each other; thus allowing high level fine-grain partitioning of resources.

It uses namespaces to isolate the network, mount, user, pid, uts, ipc, cgroup etc.. (Maybe we can cover this in another episode)

Now you might ask Why Namespaces at all ?

Lets imagine namespaces as a Box, where we can put all the related stuff in it. Now we can use this box to share it with other people. But we can also keep it private and use it for our own purpose. You can add some crazy stuffs to the box but the thing is it will not affect content of other boxes.

This helps making the code more orginised & clean.

Smol excerpt on adavantages of namespaces:

  • Code Reusability
  • Distributed Development

How to use namespaces in Typescript ?

namespace RandomNameSpaceName {}

Smol Code Example to show how we can use same variable name, same funcion name in different namespaces.

The above code allows us to use the same variable and function name without collision.

But How to access the whatever we have defined in the namespaces ?

To access the functions, variables, classes, interfaces, etc. we have to use the export keyword before the function or classname.

Let's see a simple & pratical real world application implementation of namespaces in Typescript.

Assume that we have a To-Do application.

In the above code we have declared all the possible interfaces that we might use in the application.

Note that We have 3 levels of namespaces

  • TodoApi
  • Controllers
  • Add/Get

Now let's use the decalared namespaces in the application. (ofc application doesn't work; the code is just for demonstration)

You can understand the code whole lot better, its cleaner, more orginised and easy to maintain.

At last namespaces are just set of names that are used to identify and refer to objects of various kinds.