TypeScript是否支持名称空间? [英] Does TypeScript support namespace?

查看:71
本文介绍了TypeScript是否支持名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

Typescript允许定义与ECMAScript 6中的内容紧密相关的模块.以下示例摘自该规范:

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

这等于

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.

这篇关于TypeScript是否支持名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆