TypeScript Only Features 9 exercises
explainer

Namespaces in TypeScript

Namespaces should rarely be used, but you should still be aware of them.

Namespaces were an early feature of TypeScript that let you specify spaces where you could add functions and types. This allows for names that wouldn't conflict with things declared in the global scope.

Take this example of `

Loading explainer

Transcript

00:00 Another feature of TypeScript that you should probably very rarely use are namespaces. Namespaces were an early feature of TypeScript that let you specify kind of spaces where you could add functions and types, where the names wouldn't conflict with other things like declared in the global scope.

00:17 This was before we had modules. Modules in JavaScript let you specify things within a module that can then be exported or imported into other modules. And this means that naming conflicts very rarely occur, really, because you're not putting things in the global scope. So namespaces were a way to avoid that. Here we have a namespace called GeometryUtils.

00:38 Inside that namespace, we export a namespace circle. So you can think of this huge thing up here as one big closure where there's going to be no naming conflicts within that closure.

00:51 Then you have export namespace circle, which goes down to here, which inside here has a calculateArea function and a calculateCircumference function. You can see inside here that we have a export namespace rectangle, which is being exported from GeometryUtils. If we don't export the circle, for instance, then it's not going to be available when we actually go and use it.

01:12 So you can have kind of like internal properties here, which is quite nice. Just like you can have kind of things that aren't exported from a module. So we say export namespace rectangle, and inside there, it's got an interface too. So it says export interface rectangle. So these namespaces can contain types and interfaces and things. And there we go.

01:32 And we can just use them as values if we want to. So these compile down to objects. And so we can say GeometryUtils.circle.calculateArea, perfectly fine. Or we can do them as types. So we can say GeometryUtils.rectangle.rectangle. And you notice that you get really nice autocomplete on this stuff too.

01:50 So GeometryUtils.rectangle.rectangle. It actually knows that we're in the type space here, so it only shows us the type options. Whereas out here, we would say GeometryUtils.circle.calculateArea. So there we go. This is a feature that I don't think you should be using.

02:08 It has some uses and we will explore them later in this course. But it's not great if you're just wanting to do the job that modules already do, because modules just do that job better. If you want to just create a set of utilities, maybe organize your types a little bit better, then perhaps you can use them.

02:29 But I really can't see much functionality for them, to be honest. Namespaces, I think, are kind of like a dead feature in TypeScript. You will encounter them because they're used to organize types that are in the global scope, for instance, like the Node.js types contain a lot of namespaces.

02:47 But in your application code, I would not advise using namespaces.