Does TypeScript support namespace?

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.

Leave a Comment