javascript - Typescript 外部模块,如何保持正确的类型提示?

标签 javascript intellij-idea typescript

我将在这个问题前说我正在使用 Intellij IDEA。

以下问题:If external typescript modules don't have a module name, how do you avoid naming conflicts?

这一切都很好,但是假设我在两个 Rectangle.ts 文件中有两个 Rectangle 类,但在不同的包中,例如 src/utils/geomsrc/utils/ui,或类似的东西。

src/utils/geom/Rectangle.tscalculateSurface() 作为其唯一方法,并且 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();
  1. ui 对象的智能感知选项中没有 calculateSurface 方法。
  2. ui.calculateSurface() 行存在编译错误

所以这可能是与 Intellij IDEA 或配置问题相关的错误。

关于javascript - Typescript 外部模块,如何保持正确的类型提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806086/

相关文章:

javascript - X/Y px 分辨率的 iPad 上的网站看起来不错,但在具有相同分辨率的 PC 上它太可怕了(太大)

JavaScript 在对象分配上的奇怪行为

Javascript 在 while 循环中更改不透明度不起作用

intellij-idea - 键映射 - Mac OS X 和 Mac OS X 10.5+ 之间有什么区别?

typescript - GeneratorFunction 接口(interface)在 TypeScript 中代表什么?

javascript - 使用 create-react-app 创建库

gradle - Intellij Idea - 按项目定义自定义和不同的 Gradle 用户主页

android-studio - Intellij 提取内部类

typescript - 可视代码类型脚本在空格和 tslint 上显示错误

typescript - 如何在 TypeScript 中为对象动态分配属性?