Does TypeScript support namespace?

后端 未结 5 965
无人共我
无人共我 2021-02-02 05:47

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

相关标签:
5条回答
  • 2021-02-02 06:13

    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
    
    0 讨论(0)
  • 2021-02-02 06:20

    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.

    0 讨论(0)
  • 2021-02-02 06:21

    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.

    0 讨论(0)
  • 2021-02-02 06:27

    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.

    0 讨论(0)
  • 2021-02-02 06:28

    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/

    0 讨论(0)
提交回复
热议问题