0

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

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();

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?

Community
  • 1
  • 1
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • Not sure what you mean about "another" `Rectangle` class? If you're scoping, then the only way there could be a problem is if you introduce a second `Rectangle` into the same namespace. So, maybe, I'm not clear on what you're asking. And no, it's not `module('xyz')` anymore. That was deprecated. – WiredPrairie Apr 17 '14 at 21:25
  • @WiredPrairie - I think you answered it. My concern is that rectangle.ts (or actually rectangle.js) creates a global class (and therefore object) named Rectangle which could conflict with another global object named Rectangle. Is that not the case? Nothing else can see the class unless they import it? – David Thielen Apr 17 '14 at 21:37
  • If you put `Rectangle` into a `Shapes` module for example, and import that, it would be scoped to `Shapes`. If you're using AMD or CommonJS modules, then only through a mis-configuration should you end up with a conflict. It's possible, but it would require effort. :) – WiredPrairie Apr 17 '14 at 21:42
  • @WiredPrairie Ok, thanks. ps - every time I start to think I understand typescript I trip over something else which makes me realize I don't understand it that well (yet). – David Thielen Apr 17 '14 at 22:35

1 Answers1

1

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.

For example:

import rectangle = require(./rectangle'); 

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

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

As an aside, it is require now, rather than module.

Fenton
  • 241,084
  • 71
  • 387
  • 401