Does TypeScript support namespace?

后端 未结 5 962
无人共我
无人共我 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: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.

提交回复
热议问题