54

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

5 Answers 5

56

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that these modules are now referred to as namespaces: github.com/Microsoft/TypeScript/issues/2159
I wouldn't recommended namespace nor mixing it with module source code.
28

As of version 1.5, Typescript supports namespace keyword. Namespaces are equivalent to internal modules.

From What's new in Typescript:

Before:

module Math {
    export function add(x, y) { ... }
}

After:

namespace Math {
    export function add(x, y) { ... }
}

For defining an internal module, now you can use both module and namespace.

2 Comments

Both module and namespace transpile to the same JavaScript code.
I wouldn't recommended namespace nor mixing it with module source code.
9

Here is a TypeScript namespace example:

///<reference path='AnotherNamespace/ClassOne.ts'/>
///<reference path='AnotherNamespace/ClassTwo.ts'/>

module MyNamespace
{
    import ClassOne = AnotherNamespace.ClassOne;
    import ClassTwo = AnotherNamespace.ClassTwo;

    export class Main
    {
        private _classOne:ClassOne;
        private _classTwo:ClassTwo;

        constructor()
        {
            this._classOne = new ClassOne();
            this._classTwo = new ClassTwo();
        }
    }
}

Comments

7

There is no 'namespace' keyword, but internal modules (using the 'module' keyword) and external modules (using the 'export' keyword) offer a similar way to partition your code into logical hierarchies.

2 Comments

since typescript 1.5 there is such a keyword
I wouldn't recommended namespace nor mixing it with module source code.
5

False...

module A.B.C {
    export var x = 1;
}

is equal to

module A {
    export module B {
        export module C {
            export var x = 1;
        }
    }
}

because you can write outside the module A :

var y = A.B.C.x;

But :

module A {
    module B {
        module C {
            export var x = 1;
        }
        var y = C.x; // OK
    }
    //var y = B.C.x; // Invalid
}
//var y = A.B.C.x;   // Invalid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.