As in the title: does TypeScript support namespaces? If so, how do I use them?
The namespace is used for logical grouping of functionalities. A namespace can include interfaces, classes, functions and variables to support a single or a group of related functionalities. A namespace can be created using the namespace keyword followed by the namespace name.
Use a file tsconfig.@Pavel_K In the TypeScript handbook: "To reiterate why you shouldn't try to namespace your module contents, the general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions.
A module is a way which is used to organize the code in separate files and can execute in their local scope, not in the global scope. A namespace is a way which is used for logical grouping of functionalities with local scoping.
Namespace Declaration We can create a namespace by using the namespace keyword followed by the namespace_name. All the interfaces, classes, functions, and variables can be defined in the curly braces{} by using the export keyword. The export keyword makes each component accessible to outside the namespaces.
Typescript allows to define modules closely related to what will be in ECMAScript 6. The following example is taken from the spec:
module outer { var local = 1; export var a = local; export module inner { export var x = 10; } }
As you can see, modules have names and can be nested. If you use dots in module names, typescript will compile this to nested modules as follows:
module A.B.C { export var x = 1; }
This is equal to
module A { module B { module C { export var x = 1; } } }
What's also important is that if you reuse the exact same module name in one typescript program, the code will belong to the same module. Hence, you can use nested modules to implement hierarchichal namespaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With