Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does TypeScript support namespace?

As in the title: does TypeScript support namespaces? If so, how do I use them?

like image 277
fletchsod Avatar asked Oct 05 '12 00:10

fletchsod


People also ask

How does namespace work in TypeScript?

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.

How do I import a namespace in TypeScript?

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.

What is the difference between namespace and module in TypeScript?

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.

What is declare namespace?

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.


1 Answers

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.

like image 191
Valentin Avatar answered Nov 04 '22 11:11

Valentin