Does TypeScript support namespace?

NamespacesTypescript

Namespaces Problem Overview


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

Namespaces Solutions


Solution 1 - 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.

Solution 2 - Namespaces

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.

Solution 3 - Namespaces

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();
        }
    }
}

You can check out more here:http://www.codebelt.com/typescript/javascript-namespacing-with-typescript-internal-modules/

Solution 4 - Namespaces

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.

Solution 5 - Namespaces

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfletchsodView Question on Stackoverflow
Solution 1 - NamespacesValentinView Answer on Stackoverflow
Solution 2 - NamespacesNikola ProkopićView Answer on Stackoverflow
Solution 3 - NamespacescodeBeltView Answer on Stackoverflow
Solution 4 - NamespacesRyan CavanaughView Answer on Stackoverflow
Solution 5 - NamespacesYannView Answer on Stackoverflow