0

I'll preface this question by saying I'm using Intellij IDEA.

Following this question: If external typescript modules don't have a module name, how do you avoid naming conflicts?

This is all fine and well, but let's say I'm have two Rectangle classes in two Rectangle.ts files, but in different packages, say src/utils/geom and src/utils/ui, or something like that.

src/utils/geom/Rectangle.ts has calculateSurface() as its only method and src/utils/ui/Rectangle.ts has display() as its only method.

Now if I call them both in a single file, I'll get both methods as possible calls in the type-hinting.

import GeomRectangle = require();
import UiRectangle = require();

var geom: GeomRectangle = new GeomRectangle();
var ui: UiRectangle = new UiRectangle();

// Now both those are valid
ui.calculateSurface();
ui.display();

I'm thinking it's because I'd have both Rectangle.ts files have a exports = Rectangle, since that's the name of the class and Intellij IDEA must use this export statement to determine what it'll suggest to you.

Am I wrong in my assumption? And is there any way to have type-hinting that won't trip on itself when using external modules with, sometimes, classes having the same name as each other?

Community
  • 1
  • 1
gCardinal
  • 632
  • 2
  • 9
  • 27

1 Answers1

1

I tried your code in Visual Studio:

geom/Rectangle.ts

class Rectangle {
    calculateSurface() {
       console.log("a");
    }
}

export = Rectangle;

ui/Rectangle.ts

class Rectangle {
    display() {
        console.log("b");
    }
}

export = Rectangle;

Test.ts

import GeomRectangle = require("./geom/Rectangle");
import UiRectangle = require("./ui/Rectangle");


var x: GeomRectangle = new GeomRectangle();
var ui: UiRectangle = new UiRectangle();

// Now both those are valid
ui.calculateSurface(); // compilation error here
ui.display();
  1. There is no calculateSurface method in the intellisense options of the ui object.
  2. There is compilation error on the ui.calculateSurface() line

So it probably could be bug related to Intellij IDEA or configuration issue.

Max Brodin
  • 3,903
  • 1
  • 14
  • 23
  • So I'm back from my weekend and it works. I'm not sure if anything was done, no commit was pushed, but it works. Maybe Intellij IDEA's cache was in the way? No idea, but I'll keep an eye out for what happened. Thank you for taking the time to answer my question! – gCardinal Jun 15 '15 at 13:59