模块扩充错误:“x”仅指类型,但在此处用作值

gaw*_*cks 6 typescript protractor angular

我对此有点陌生。如果我弄错了,请原谅。我正在尝试执行此操作以在量角器 ElementFinder 类原型上定义一个方法。

我在这里遵循合并拉取请求中的语法。https://github.com/Microsoft/TypeScript/pull/6213

> 1 import { ElementFinder} from "protractor";
> 2 
> 3 declare module "protractor" {
> 4   interface ElementFinder {
> 5        doSomething(): void;
> 6    } }
> 7
> 8
> 9  ElementFinder.prototype.doSomething = function (): void {
> 10    console.log(""); 
> 11 };
Run Code Online (Sandbox Code Playgroud)

我在第 9 行收到此错误

[ts] 'ElementFinder' 仅指类型,但在这里用作值。

我在这里做错了什么?该示例中唯一不同的是,我使用的是 npm 中的模块,而不是我的包中定义的模块。是否不可能以这种方式增强模块?

jca*_*alz 5

它看起来像是ElementFinder输入为 aninterface而不是 a class。似乎没有名为ElementFinder您可以引用来获取其原型的导出对象。ElementFinderProtractor在运行时给你一个构造函数吗?如果是这样,您可以这样输入:

type Constructor<T> = {
    new (...args: any[]): T;
    readonly prototype: T;
}
declare module "protractor" {
  interface ElementFinder {
    doSomething(): void;
  }
  export const ElementFinder: Constructor<ElementFinder>;
}
Run Code Online (Sandbox Code Playgroud)

应该可以修复类型错误,但请确保ElementFinder运行时实际上有一个构造函数,否则当您运行它时它会爆炸。