如果外部打字稿模块没有模块名,你怎么避免命名冲突? [英] If external typescript modules don't have a module name, how do you avoid naming conflicts?

查看:139
本文介绍了如果外部打字稿模块没有模块名,你怎么避免命名冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续(排序)约<一个问题href=\"http://stackoverflow.com/questions/12841557/whats-the-difference-between-internal-and-external-modules-in-typescript\">the内部和外部模块之间的差异。

This is a follow-on (sort of) to the question about the differences between internal and external modules.

我想因为我们将有一个非常大的程序来使用外部模块。问题是,如果我创建一个类rectangle.ts:

I want to use external modules as we're going to have a very large program. The problem is, if I create a class rectangle.ts:

   export class Rectangle {

        public x : number;
        public y : number;
        public width : number;
        public height : number;
    }

,然后使用访问:

And then access it using:

import rectangle = require("rectangle"); 
// or is it "import rectangle = module("rectangle");"?
var rect = new rectangle.Rectangle();

如果有另一个Rectangle类那里会发生什么?我是否需要担心命名空间的冲突?还是我安全的,因为我有电话,要求和现在用的.TS文件,明确指定类并没有什么摆在全局命名空间?

What happens if there's another Rectangle class out there? Do I need to worry about namespace conflicts? Or am I safe because I have the call to require and am using the .ts file to clearly specify the class and there is nothing put in the global namespace?

推荐答案

如果你有许多矩形模块,你必须给它们命名选项然而,当你导入它们喜欢。该模块只放入您的别名,他们不污染,否则你的范围。

If you were to have many rectangle modules, you have the option to name them however you like when you import them. The module is only placed into your alias, they don't otherwise pollute your scope.

例如:

import rectangle = require(./rectangle'); 

import differentRectangle = require('./folder/rectangle');

var rect = new rectangle.Rectangle();
var rectB = new differentRectangle.Rectangle();

顺便说一句,这是要求现在,而不是模块

这篇关于如果外部打字稿模块没有模块名,你怎么避免命名冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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