我将在这个问题前说我正在使用 Intellij IDEA。
以下问题:If external typescript modules don't have a module name, how do you avoid naming conflicts?
这一切都很好,但是假设我在两个 Rectangle.ts 文件中有两个 Rectangle 类,但在不同的包中,例如 src/utils/geom
和 src/utils/ui
,或类似的东西。
src/utils/geom/Rectangle.ts
将 calculateSurface()
作为其唯一方法,并且 src/utils/ui/Rectangle.ts
> 将 display()
作为其唯一方法。
现在,如果我在一个文件中调用它们,我将在类型提示中将这两种方法作为可能的调用。
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();
我想这是因为我的两个 Rectangle.ts 文件都有一个 exports = Rectangle
,因为这是类的名称,并且 Intellij IDEA 必须使用此导出语句来确定它的内容会向您推荐。
我的假设有错吗?有没有什么方法可以让类型提示在使用外部模块(有时,类彼此具有相同名称)时不会自行出错?
最佳答案
我在 Visual Studio 中尝试了您的代码:
geom/矩形.ts
class Rectangle {
calculateSurface() {
console.log("a");
}
}
export = Rectangle;
ui/矩形.ts
class Rectangle {
display() {
console.log("b");
}
}
export = Rectangle;
测试.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();
ui
对象的智能感知选项中没有calculateSurface
方法。ui.calculateSurface()
行存在编译错误
所以这可能是与 Intellij IDEA
或配置问题相关的错误。
关于javascript - Typescript 外部模块,如何保持正确的类型提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806086/