4

I try to use nightmare in typescript with the types from DefinitelyTyped. however, the types there are incomplete (the then method is missing) and I also want to add dynamically some methods (as it is done in this plugin).

I don't understand how to extend the interface of Nightmare without forking DefinitelyTyped. I have read a few pages about these question:

Extend interface defined in .d.ts file

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

So I tried to create a extendNightmare.d.ts file that I put along all my other .ts files.

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

At this point, I don't know what to do to tell the compiler to use my new definition that is supposed to extend the existing one.

I tried to add this line at the top of the file using nightmare but it didn't work:

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

Also I would like to extend the interface IConstructorOptions from the Nightmare namespace, but I don't know how to do so. Is this correct?

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

  export interface IConstructorOptions {
    switches: any;
  }
}

The code I'm using to reproduce this issue:

/// <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");

In my package.json, I have:

"nightmare": "^2.9.1",
"@types/nightmare": "^1.6.30",
Community
  • 1
  • 1
Louis Roché
  • 876
  • 1
  • 7
  • 19

0 Answers0