[英]Typescript external modules, how to keep proper type-hinting?
I'll preface this question by saying I'm using Intellij IDEA. 我将通过说我正在使用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. 一切都很好,但是假设我在两个Rectangle.ts文件中有两个Rectangle类,但是在不同的包中,例如
src/utils/geom
和src/utils/ui
或类似的东西。
src/utils/geom/Rectangle.ts
has calculateSurface()
as its only method and src/utils/ui/Rectangle.ts
has display()
as its only method. src/utils/geom/Rectangle.ts
将calculateSurface()
作为其唯一方法,而src/utils/ui/Rectangle.ts
将display()
作为其唯一方法。
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. 我在想这是因为我两个Rectangle.ts文件都有一个export
exports = Rectangle
,因为那是该类的名称,因此Intellij IDEA必须使用此export语句来确定对您的建议。
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?
并且,当使用带有彼此相同名称的类的外部模块时,是否有任何类型的提示不会自行跳动?
I tried your code in Visual Studio: 我在Visual Studio中尝试了您的代码:
geom/Rectangle.ts geom / Rectangle.ts
class Rectangle {
calculateSurface() {
console.log("a");
}
}
export = Rectangle;
ui/Rectangle.ts 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();
calculateSurface
method in the intellisense options of the ui
object. ui
对象的intellisense选项中没有任何calculateSurface
方法。 ui.calculateSurface()
line ui.calculateSurface()
行上存在编译错误 So it probably could be bug related to Intellij IDEA
or configuration issue. 因此,可能是与
Intellij IDEA
或配置问题有关的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.