TypeScript 支持命名空间吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12737942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:49:48  来源:igfitidea点击:

Does TypeScript support namespace?

namespacestypescript

提问by fletchsod

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

如标题所示:TypeScript 支持命名空间吗?如果是这样,我该如何使用它们?

回答by Valentin

Typescript allows to define modulesclosely related to what will be in ECMAScript 6. The following example is taken from the spec:

Typescript 允许定义与 ECMAScript 6 中的内容密切相关的模块。以下示例取自规范:

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.

同样重要的是,如果您在一个打字稿程序中重用完全相同的模块名称,则代码将属于同一个模块。因此,您可以使用嵌套模块来实现分层命名空间。

回答by Nikola Prokopi?

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

从 1.5 版开始,Typescript 支持namespace关键字。命名空间相当于内部模块。

From What's new in Typescript:

来自Typescript 的新内容

Before:

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

After:

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

前:

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

后:

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

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

要定义内部模块,现在您可以同时使用modulenamespace

回答by codeBelt

Here is a TypeScript namespace example:

这是一个 TypeScript 命名空间示例:

///<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/

您可以在此处查看更多信息:http: //www.codebelt.com/typescript/javascript-namespacing-with-typescript-internal-modules/

回答by Ryan Cavanaugh

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.

没有 'namespace' 关键字,但内部模块(使用 'module' 关键字)和外部模块(使用 'export' 关键字)提供了类似的方式来将代码划分为逻辑层次结构。

回答by Yann

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 :

因为你可以在模块 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