扩展DefinitelyTyped .d.ts定义

时间:2017-05-03 03:44:09

标签: typescript interface extends nightmare definitelytyped

我尝试在typescript中使用来自DefinitelyTyped的类型的nightmare。但是,那里的类型不完整(缺少then方法),我还想动态添加一些方法(就像this plugin中所做的那样)。

我不明白如何在不牺牲DefinitelyTyped的情况下扩展Nightmare的界面。我已经阅读了几页关于这些问题:

Extend interface defined in .d.ts file

http://obaidurrehman.github.io/2015/10/30/extending-typeScript-definitions/

所以我尝试创建一个extendNightmare.d.ts文件,放在我所有其他.ts个文件中。

declare namespace Nightmare {
  class Nightmare {
    waitForDevTools(): void;
    then(): Nightmare;
  }
}

此时,我不知道如何告诉编译器使用我的新定义来扩展现有定义。

我尝试使用噩梦在文件顶部添加此行,但它不起作用:

/// <reference path="./extendNightmare.d.ts"/>

此外,我想从IConstructorOptions命名空间扩展接口Nightmare,但我不知道如何操作。这是对的吗?

declare namespace Nightmare {
  class Nightmare {
    waitForDevTools(): void;
    then(): Nightmare;
  }

  export interface IConstructorOptions {
    switches: any;
  }
}

我用来重现此问题的代码:

/// <reference path="./extendNightmare.d.ts"/>

import * as Nightmare from "nightmare";

const switches = {
  "ignore-certificate-errors": true,
  "proxy-server": "localhost:10000",
};

function render_page(url) {
  const options = {
    switches,
  };

  const nightmare = new Nightmare(options);

  nightmare
  .goto(url)
  .then((infos) => {
    console.info(infos);
  })
  .end()
  .then(() => {
    console.info(`${url}: done`);
  });
}

render_page("https://google.com");

在我的package.json中,我有:

"nightmare": "^2.9.1",
"@types/nightmare": "^1.6.30",

0 个答案:

没有答案
相关问题