简体   繁体   English

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

[英]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/geomsrc/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.tscalculateSurface()作为其唯一方法,而src/utils/ui/Rectangle.tsdisplay()作为其唯一方法。

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();
  1. There is no calculateSurface method in the intellisense options of the ui object. ui对象的intellisense选项中没有任何calculateSurface方法。
  2. There is compilation error on the 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.

相关问题 如何使WebStorm识别用于类型提示和警告的JavaScript“窗口”对象? - How do I make WebStorm recognize the JavaScript “window” object for type-hinting and warnings? JetBrains IDE 上的 Javascript:类型提示自主自定义元素实例 - Javascript on JetBrains IDE: type-hinting the autonomous custom element instance 编译TypeScript时如何处理外部模块? - How to deal with external modules when compiling TypeScript? Angular + TypeScript +外部模块。 如何跨文件维护类型信息? - Angular + TypeScript + External Modules. How to maintain type information across files? vue 3和typescript中如何正确键入vuex模块 - How to correctly type vuex modules in vue 3 and typescript 在使用个人和外部模块时如何编译打字稿? - How to compile down typescript while using personal and external modules? 如何将现有的打字稿命名空间类用作Node.js的外部模块 - How to use Existing typescript namespaced classes as external modules for nodejs 如何使用Typescript的外部模块动态实例化 - How do dynamically instantiate using typescript's external modules 如何在 TypeScript 外部模块中使用命名空间? - How do I use namespaces with TypeScript external modules? 打字稿泛型未返回正确的类型 - Typescript Generics not returning proper type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM